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版本切换工具pyenv的安装及用法

《python版本切换工具pyenv的安装及用法》Pyenv是管理Python版本的最佳工具之一,特别适合开发者和需要切换多个Python版本的用户,:本文主要介绍python版本切换工具pyen... 目录Pyenv 是什么?安装 Pyenv(MACOS)使用 Homebrew:配置 shell(zsh

Java中的CompletableFuture核心用法和常见场景

《Java中的CompletableFuture核心用法和常见场景》CompletableFuture是Java8引入的强大的异步编程工具,支持链式异步编程、组合、异常处理和回调,介绍其核心用法,通过... 目录1、引言2. 基本概念3. 创建 CompletableFuture3.1. 手动创建3.2.

MySQL中between and的基本用法、范围查询示例详解

《MySQL中betweenand的基本用法、范围查询示例详解》BETWEENAND操作符在MySQL中用于选择在两个值之间的数据,包括边界值,它支持数值和日期类型,示例展示了如何使用BETWEEN... 目录一、between and语法二、使用示例2.1、betwphpeen and数值查询2.2、be

Java序列化之serialVersionUID的用法解读

《Java序列化之serialVersionUID的用法解读》Java序列化之serialVersionUID:本文介绍了Java对象的序列化和反序列化过程,强调了serialVersionUID的作... 目录JavChina编程a序列化之serialVersionUID什么是序列化为什么要序列化serialV

python3中正则表达式处理函数用法总结

《python3中正则表达式处理函数用法总结》Python中的正则表达式是一个强大的文本处理工具,用于匹配、查找、替换等操作,在Python中正则表达式的操作主要通过内置的re模块来实现,这篇文章主要... 目录前言re.match函数re.search方法re.match 与 re.search的区别检索

MySQL 中的 JSON_CONTAIN用法示例详解

《MySQL中的JSON_CONTAIN用法示例详解》JSON_CONTAINS函数用于检查一个JSON文档中是否包含另一个JSON文档,这篇文章给大家介绍JSON_CONTAINS的用法、语法、... 目录深入了解 mysql 中的 jsON_CONTAINS1. JSON_CONTAINS 函数的概述2

JDK21对虚拟线程的几种用法实践指南

《JDK21对虚拟线程的几种用法实践指南》虚拟线程是Java中的一种轻量级线程,由JVM管理,特别适合于I/O密集型任务,:本文主要介绍JDK21对虚拟线程的几种用法,文中通过代码介绍的非常详细,... 目录一、参考官方文档二、什么是虚拟线程三、几种用法1、Thread.ofVirtual().start(

Java8 Collectors.toMap() 的两种用法

《Java8Collectors.toMap()的两种用法》Collectors.toMap():JDK8中提供,用于将Stream流转换为Map,本文给大家介绍Java8Collector... 目录一、简单介绍用法1:根据某一属性,对对象的实例或属性做映射用法2:根据某一属性,对对象集合进行去重二、Du

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数

Python中的sort方法、sorted函数与lambda表达式及用法详解

《Python中的sort方法、sorted函数与lambda表达式及用法详解》文章对比了Python中list.sort()与sorted()函数的区别,指出sort()原地排序返回None,sor... 目录1. sort()方法1.1 sort()方法1.2 基本语法和参数A. reverse参数B.