在angular4中,引入如JQuery和Bootstrap之类的库时,有时候不起作用,下面我们先来看一下一般我们会怎样做:
1、安装第三方库 npm install bootstrap --save
2、在angular_cli.json中添加:
"styles": [
"styles.css",
"../node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
"../node_modules/jquery/dist/jquery.min.js",
"../node_modules/bootstrap/dist/js/bootstrap.min.js"
],
3、安装类型描述文件: npm install @types/jquery --save-dev
但是发现运行后完全没有效果,原因出在第2步上,第2步引入的库是不带版本号的,实际上应该引入带版本号的,这个问题就解决了。
"styles": [
"styles.css",
"../node_modules/_bootstrap@3.3.7@bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
"../node_modules/_jquery@3.2.1@jquery/dist/jquery.min.js",
"../node_modules/_bootstrap@3.3.7@bootstrap/dist/js/bootstrap.min.js"
],