plop的用法

2024-05-05 09:18
文章标签 用法 plop

本文主要是介绍plop的用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

每次写重复的代码是不是很浪费时间呢?接下来介绍一款用命令行就可以自动生成代码的工具。

plop的介绍 https://www.npmjs.com/package/plop

1.在项目中安装plop;

npm install --save-dev plop

2.全局安装,这样就可以用plop命令了;

npm install -g plop

mac 使用 

sudo npm install -g plop

3.在项目的根目录创建plop.js文件,写入一下代码;

module.exports = function (plop) {plop.setGenerator('component', {description: '视图组件',prompts: [{type: 'input',name: 'name',message: '组件的名字, 如MyApp',validate: function (value) {if ((/([A-Z][a-z]+)+/).test(value)) { return true; }return '组件名称必须为驼峰形式';}}],actions: [/*** TemplateComponent.js*/{type: 'add',path: 'src/component/{{name}}/{{name}}.js',templateFile: 'templates/components/TemplateComponent.js'},{type: 'modify',path: 'src/component/{{name}}/{{name}}.js',pattern: /TemplateComponent/g,template: '{{name}}'},{type: 'modify',path: 'src/component/{{name}}/{{name}}.js',pattern: /template-component/g,template: '{{dashCase name}}'},/*** template-component.scss and css*/{type: 'add',path: 'src/component/{{name}}/{{dashCase name}}.css',templateFile: 'templates/components/template-component.css'},{type: 'modify',path: 'src/component/{{name}}/{{dashCase name}}.css',pattern: /TemplateComponent/g,template: '{{dashCase name}}'},{type: 'modify',path: 'src/component/{{name}}/{{dashCase name}}.css',pattern: /template-component/g,template: '{{dashCase name}}'},]});plop.setGenerator('router', {description: '路由生成器',prompts: [{type: 'list',name: 'rootPath',message: '生成路由的目录',choices: ['Routes']}, {type: 'input',name: 'routerPath',message: '路由的名字, 全部小写,用下划线分词 如:orders'}],actions: function(data){console.log(data);return [{// 配置路由文件type: 'modify',path: 'src/{{rootPath}}/index.js',pattern: /\/\/ generator import/,template: "import {{pascalCase routerPath }} from './{{ routerPath }}';\n// generator import"}, {type: 'modify',path: 'src/{{rootPath}}/index.js',pattern: /{ \/\* generator router \*\/ }/,template: '<Route path="/{{ routerPath }}"       component={ {{pascalCase routerPath}} }        desc="TODO: 该路由描述" />\n      { /* generator router */ }'}, {// 配置路由内容type: 'add',path: 'src/{{rootPath}}/{{routerPath}}/index.js',templateFile: 'templates/router/index.js'}, {type: 'add',path: 'src/{{rootPath}}/{{routerPath}}/{{pascalCase routerPath}}List.js',templateFile: 'templates/router/list.js'}, {type: 'add',path: 'src/{{rootPath}}/{{routerPath}}/{{pascalCase routerPath}}Detail.js',templateFile: 'templates/router/detail.js'}];}});
};

 

 

4.在根目录新建templates文件,在templates文件下新建components和router文件

5.在components下新建template-component.css和Templatecomponent.js文件

template-component.css
@keyframes fadeInUp {from {opacity: 0;transform: translate3d(0, 10px, 0); }to {opacity: 1;transform: none; } }.TemplateComponent {animation-name: fadeInUp;animation-duration: 1s;animation-fill-mode: both;display: flex;flex: 1; }.TemplateComponent *, .TemplateComponent :after, .TemplateComponent :before {box-sizing: border-box; }.TemplateComponent .fl {float: left; }.TemplateComponent .fr {float: right; }.TemplateComponent .clearfix:after {content: ".";display: block;height: 0;clear: both;visibility: hidden; }.TemplateComponent .clearfix {display: inline-block; }.TemplateComponent * html .clearfix {height: 1%; }.TemplateComponent .clearfix {display: block; }.TemplateComponent ul li:hover {background: #f63;cursor: pointer; }
Templatecomponent.js
/*** Created by ${USER} on ${DATE}.* https://www.jetbrains.com/help/webstorm/file-template-variables.html 动画callback只支持1.x版本的TransitionGroup*/
import React,{Component} from 'react';
import './template-component.css';
const styles = {container: {}
};
//import ReactDOM from 'react-dom';
//import {TweenMax} from "gsap";
//import PropTypes from 'prop-types';class TemplateComponent extends React.Component {static defaultProps = {...Component.defaultProps}static propTypes = {}constructor(props){super(props)this.state = {}this.dom=React.createRef()//React.createRef();current//事件绑定在es6中用于自定义事件props事件不适用//this.handleClick = this.handleClick.bind(this);}//组件将要装载//componentWillMount(){}//组件加载完毕componentDidMount(){//this.dom.root=ReactDOM.findDOMNode(this);}//组件将接收道具//componentWillReceiveProps(nextProps){}//shouldComponentUpdate(nextProps, nextState) {}//组件将更新//componentWillUpdate(nextProps, nextState){}//组件更新完毕//componentDidUpdate(nextProps, nextState){}//组件将要卸载//componentWillUnmount(){}/*动画*///componentWillAppear(callback){}//componentDidAppear(){}//componentWillEnter(callback){}//componentDidEnter(){}//componentWillLeave(callback){}//componentDidLeave(){}render() {return (<div ref={this.dom}></div>);}
}export default TemplateComponent;

组件的模板就是以上,还可以根据自身需要定制路由。

6.在terminal中输入plop,就会让你选择是要生成组件还是路由,可根据需要选择,键入enter,再输入组件名称,就可以在模板中设置好的路径中找到文件,是不是很方便呢?

这篇关于plop的用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/961341

相关文章

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

Java中HashMap的用法详细介绍

《Java中HashMap的用法详细介绍》JavaHashMap是一种高效的数据结构,用于存储键值对,它是基于哈希表实现的,提供快速的插入、删除和查找操作,:本文主要介绍Java中HashMap... 目录一.HashMap1.基本概念2.底层数据结构:3.HashCode和equals方法为什么重写Has

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?

Python库 Django 的简介、安装、用法入门教程

《Python库Django的简介、安装、用法入门教程》Django是Python最流行的Web框架之一,它帮助开发者快速、高效地构建功能强大的Web应用程序,接下来我们将从简介、安装到用法详解,... 目录一、Django 简介 二、Django 的安装教程 1. 创建虚拟环境2. 安装Django三、创

python中update()函数的用法和一些例子

《python中update()函数的用法和一些例子》update()方法是字典对象的方法,用于将一个字典中的键值对更新到另一个字典中,:本文主要介绍python中update()函数的用法和一些... 目录前言用法注意事项示例示例 1: 使用另一个字典来更新示例 2: 使用可迭代对象来更新示例 3: 使用

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除