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

相关文章

SQL BETWEEN 语句的基本用法详解

《SQLBETWEEN语句的基本用法详解》SQLBETWEEN语句是一个用于在SQL查询中指定查询条件的重要工具,它允许用户指定一个范围,用于筛选符合特定条件的记录,本文将详细介绍BETWEEN语... 目录概述BETWEEN 语句的基本用法BETWEEN 语句的示例示例 1:查询年龄在 20 到 30 岁

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

java中Optional的核心用法和最佳实践

《java中Optional的核心用法和最佳实践》Java8中Optional用于处理可能为null的值,减少空指针异常,:本文主要介绍java中Optional核心用法和最佳实践的相关资料,文中... 目录前言1. 创建 Optional 对象1.1 常规创建方式2. 访问 Optional 中的值2.1

git stash命令基本用法详解

《gitstash命令基本用法详解》gitstash是Git中一个非常有用的命令,它可以临时保存当前工作区的修改,让你可以切换到其他分支或者处理其他任务,而不需要提交这些还未完成的修改,这篇文章主要... 目录一、基本用法1. 保存当前修改(包括暂存区和工作区的内容)2. 查看保存了哪些 stash3. 恢

Python struct.unpack() 用法及常见错误详解

《Pythonstruct.unpack()用法及常见错误详解》struct.unpack()是Python中用于将二进制数据(字节序列)解析为Python数据类型的函数,通常与struct.pa... 目录一、函数语法二、格式字符串详解三、使用示例示例 1:解析整数和浮点数示例 2:解析字符串示例 3:解