我是Angular的新手,安装了最新版本的Angular。我还集成了管理主题,其中有bootstrap.css,其他CSS和其他JS包括jQuery。我在谷歌上搜索了“如何在Angular中使用外部.js文件”,但没有一个解决方案有效。我已经通过npm install安装了jQuery、Bootstrap和其他jQuery插件js
下面是我的angular.json文件。
"scripts": [
"src/assets/js/main.js",
"node_modules/jquery/dist/jquery.js",
"src/assets/vendor/jquery-ui.min.js",
"node_modules/animsition/dist/js/animsition.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js",
"node_modules/popper.js/dist/popper.min.js"
]发布于 2019-07-19 19:41:49
您可以在组件或模块级别加载js。
步骤1:在component.ts文件中
public loadScript(url) {
let node = document.createElement('script');
node.src = url;
node.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(node);
}步骤2:在ngOnInit()中
this.loadScript("assets/js/script.js");这将动态加载外部js。这对我来说很好
发布于 2019-07-20 20:50:13
在加载jQuery之前,您正在加载外部js文件,您需要先加载jQuery,这样外部js文件中的jQuery代码才能工作,请检查下面的顺序
"scripts": [
"node_modules/jquery/dist/jquery.js",
"src/assets/vendor/jquery-ui.min.js",
"node_modules/animsition/dist/js/animsition.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js",
"node_modules/popper.js/dist/popper.min.js",
"src/assets/js/main.js"
]发布于 2019-02-24 00:30:14
我怀疑您到node_modules的路径应该是相对的- "../node_modules/
https://stackoverflow.com/questions/54842244
复制相似问题