【个人笔记重点,不作为参考】主题:angular-cli

推荐:

https://www.sitepoint.com/ultimate-angular-cli-reference/        (angular-cli的详细指令说明)/*可深入了解*/


说明:

angular:https://angular.cn/docs/ts/latest/cli-quickstart.html    (说明:介绍angular-cli,如文件的意义,用途,如何安装)

github :https://github.com/angular/angular-cli                      (说明:可以了解angular-cli的工作原理)


题外:

coreUI(提供样式的插件):http://coreui.io/docs/getting-started/introduction


---------------------------------------------------------------

angular-cli引用第三方库转载自:http://www.cnblogs.com/baiyangcao/archive/2017/03/15/angular-cli-json.html

Angular-Cli中引用第三方库

最近在学习angular(AngularJS 2),根据教程使用angular-cli新建项目,
然而在添加JQuery和Bootstrap第三方库时遇到了问题...

初试

我最初的想法是直接将相对路径写到index.html即可,如下:

<link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.min.css" />
<script type="text/javascript" src="../node_modules/jquery/dist/jquery.min.js"/>
<script type="text/javascript" src="../node_modules/bootstrap/dist/js/bootstrap.min.js"/>

然鹅。。。并不好使,浏览器抓包会显示请求
http://localhost:4200/node_modules/juqery/dist/jquery.min.js返回404错误,
bootstrap也是相同的问题,这里显然是路径不正确,我的项目目录结构如下:

angular-form/
    |- src/
    |   |- app/
    |   |- index.html
    |   ...
    |- node_modules
    |   |- jquery/
    |   |- bootstrap/
    |   ...

其中,网站运行时的根目录是src目录,
所以获取不到与其处在同一目录的node_modules目录下文件也在情理之中...

另辟蹊径

经过乱七八糟的查找...发现了可以在/.angular-cli.json文件中配置脚本引用,
在其app.scripts下配置要添加的脚本,
并在app.styles下配置要添加的样式文件:

"app": [
    {
        ...
        "styles": [
            "node_modules/bootstrap/dist/css/bootstrap.min.css"
        ],
        "scripts": [
            "node_modules/bootstrap/dist/css/bootstrap.min.css",
            "node_modules/bootstrap/dist/css/bootstrap.min.css"
        ],
        ...
    }
]

再次启动网站,却连编译都无法通过...出现如下问题:

ERROR in multi script-loader!./src/~/jquery/dist/jquery.min.js script-loader!./src/~/bootstrap/dist/js/bootstrap.min.js
Module not found: Error: Can't resolve 'E:\Code\JavaScript\angular2\angular-forms\src\node_modules\jquery\dist\jquery.min.js' in 'E:\Code\JavaScript\angular2\angular-forms'
 @ multi script-loader!./src/~/jquery/dist/jquery.min.js script-loader!./src/~/bootstrap/dist/js/bootstrap.min.js

可以看出这里去加载js脚本时寻找的是src/目录下的node_modules目录,
所以加载失败。这意味着angular-cli.json文件中配置的路径时相对于网站根目录的路径,
接着做如下更改:

"app": [
    {
        ...
        "styles": [
            "../node_modules/bootstrap/dist/css/bootstrap.min.css"
        ],
        "scripts": [
            "../node_modules/bootstrap/dist/css/bootstrap.min.css",
            "../node_modules/bootstrap/dist/css/bootstrap.min.css"
        ],
        ...
    }
]

再次运行网站,成功加载~~~

回看来时路

后来了解到,angular-cli的项目使用webpack来将模块打包,
我们这里配置的scriptsstyles会被打包成scripts.bundle.js
styles.bundle.js文件加载到前台页面,而后就阔以正常使用这些第三方库了~~~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值