Angular4.x 引入第三方 JS

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24078843/article/details/77750633

引入 Jquery

使用 angular-cli 新建 angular项目

1、安装 jquery

npm install –save jquery

2、.angular-cli.json 中引入 jquery.min.js

“scripts”: [“../node_modules/jquery/dist/jquery.min.js”]

3、安装 jquery的 typescript 类型声明文件

npm install –save @types/jquery

4、tsconfig.app.json 引入 jquery 的类型声明文件

“types”: [ “../node_modules/@types/jquery/index.d.ts”]
你也可以直接这么做:”types”:[“jquery”]

5、angluar 组件中使用 jquery 给 button 添加事件

ngOnInit(): void {
    $(() => {
      $('#btn_test').click(() => {
        alert('test');
      });
    });
  }

另一种方案,我猜你会更喜欢


1.下载 jquery.min.js 放到 assets/jquery下
2.引入jquery: “scripts”: [“assets/jquery/jquery.min.js”]
3.在 typings.d.ts 下新增两句

declare var $: any;
declare var jQuery: any;

4.这样你就可以直接使用 $ 了,当然你也可以通过这样的方式引入 bootstrap 而不是使用 npm 安装引入

其他参考: http://blog.csdn.net/mingyueyixi/article/details/73695321

猜你喜欢

转载自blog.csdn.net/qq_24078843/article/details/77750633