在Angular-cli中使用Pug(Jade)模板
来源:互联网 发布:公文的阅知范围体现在 编辑:程序博客网 时间:2024/04/30 10:54
之前介绍过如何在Angular项目中使用Pug模板,但是方法不是很“优雅”,暴露了不必要的loader模块,且Pug模板编写方法与HTML不尽相同,部分属性需要引号包起来。
Angular实际是使用webpack进行打包的,那就可以直接修改webpack.config.js文件,添加pug文件的loader。
直接写步骤吧。
1 npm install pug-ng-html-loader --save-dev
。
2 在项目的根目录下新建文件pug-rule-insert.js
,内容为
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 执行node pug-rule-insert.js
4 修改templateUrl属性,比如改为’./my-component.template.pug’。
5 修改package.json文件,新增postintall钩子,这样每次install完成之后都会自动修改webpack配置文件
scripts:{ ... "postinstall": "node pug-rule-insert.js"}
6 执行ng serve
查看效果
参考文章:https://medium.com/@MarkPieszak/using-pug-or-jade-templates-with-the-angular-cli-9e37334db5bc
本文使用的是该文章评论中提到的pug-ng-html-loader, 解决了pug编译html导致angular不兼容的问题
阅读全文
0 0
- 在Angular-cli中使用Pug(Jade)模板
- 在angular-cli项目中使用Scss和Pug(Jade)模板引擎
- Angular4 中使用Pug/Jade
- Jade/pug-HTML模板引擎
- Webpack 3.x 尝试使用Pug(Jade)模板引擎
- jade (pug)
- 如何在 WebStorm 使用 Angular CLI 建立 Angular 2
- pug模板的使用及终端
- WebStorm添加jade(pug) watcher
- Jade 模板引擎使用
- Jade 模板引擎使用
- jade模板引擎使用中遇到的坑
- jade模板引擎使用记录
- Express中使用Jade
- Angular-使用Angular CLI快速搭建框架
- angular-cli下使用bootstrap
- angular-cli 安装与使用
- 使用Angular CLI创建项目
- 打开Android Studio报错“Error running ***: Please select Android SDK”
- Day06 --面向对象
- [NOI2014]起床困难综合症
- 找座椅
- 浅谈重构中踩过的坑
- 在Angular-cli中使用Pug(Jade)模板
- linux下文件的创建时间、访问时间、修改时间和改变时间
- 安装macvim
- 单例7种写法
- Android内核开发:图解Android系统的启动过程
- SpringMVC基本注解的介绍
- java代码启动tomcat报The CATALINA_HOME enironment variable is not defined correctly.....错误
- Python基础 urllib
- 从银行转账失败到分布式事务:总结与思考