在Angular-cli中使用Pug(Jade)模板
之前介绍过如何在Angular项目中使用Pug模板,但是方法不是很“优雅”,暴露了不必要的loader模块,且Pug模板编写方法与HTML不尽相同,部分属性需要引号包起来。 Angular实际是使用webpack进行打包的,那就可以直接修改webpack.config.js文件,添加pug文件的loader。 直接写步骤吧。 1 const fs = require('fs');
const commonCliConfig = 'node_modules/@angular/cli/models/webpack-configs/common.js';
const pug_rule = `n{ test: /.(pug|jade)$/,loader: "pug-ng-html-loader" },`;
fs.readFile(commonCliConfig,(err,data) => { if (err) { throw err; } const configText = data.toString(); // make sure we don't add the rule if it already exists if (configText.indexOf(pug_rule) > -1) { return; } // We made it this far,let's insert that pug webpack rule const position = configText.indexOf('rules: [') + 8; const output = [configText.slice(0,position),pug_rule,configText.slice(position)].join(''); const file = fs.openSync(commonCliConfig,'r+'); fs.writeFile(file,output); // ta-da fs.close(file); });
这实际是修改了webpack打包文件。 3 执行 scripts:{
...
"postinstall": "node pug-rule-insert.js"
}
6 执行 参考文章:https://medium.com/@MarkPieszak/using-pug-or-jade-templates-with-the-angular-cli-9e37334db5bc 本文使用的是该文章评论中提到的pug-ng-html-loader,解决了pug编译html导致angular不兼容的问题 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- angularjs – 以编程方式单击以更改UI-Bootstrap中的当前选
- 单元测试 – AngularJS测试在promise中调用了promise
- scala – Apache-Spark:什么是map(_._ 2)的简写?
- 在Scala的Monocle中过滤列表
- Bagging(Bootstrap aggregating)、随机森林(random fore
- yum安装MySQL5.6
- webservice与remoting
- angular – 在router.navigate之后没有调用ngOnInit
- elasticsearch – 在Docker容器中保留弹性搜索数据
- webservice大文件传输