开门见山
在前端工具
-自定义generator文章中,介绍了如何自定义generator模块,以及生成对应的文件,如果是一个接一个写文件的话
比较费力,好在我们有模板语法通过模板语法,大大提高了效率
模板语法
const Generator
= require('yeoman-generator');
module
.exports
= class extends Generator
{
const templ
= this.templatePath('foo.txt');
const output
= this.destinationPath('foo.txt');
const content
= {title
:"hello world",success
:"success"}
this.fs
.copyTpl(templ
,output
,consent
)
}
接收用户输出
不知道大家有没有印象,在vue使用过程中,有需要输入项目名称,那么是怎么做到的呢,其实就是接收用户输入信息而已
为大家演示一下
const Generator
= require('yeoman-generator');
module
.exports
= class extends Generator
{
prompting(){
return this.prompt([{
type
: "input",
name
: "name",
message
: "Your project name",
default: this.appname
}]).then(answer
=> {
this.answer
= answer
})
}
const templ
= this.templatePath('foo.txt');
const output
= this.destinationPath('foo.txt');
const content
= this.answer
this.fs
.copyTpl(templ
,output
,consent
)
}
谢谢观看,如有不足,敬请指教