angular4引入jQuery和基於jQuery的插件!

1、angular4單純的引入jQuery
方法1:
index.html 直接引入路徑,cdn路徑或者assets文件下的jQuery文件均可以。例如:html

<script src="http://code.jquery.com/jquery-2.2.4.js"   integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI="   crossorigin="anonymous"></script>

或者在.angular-cli.json文件中這樣添加代碼jquery

"scripts": [
    "assets/jquery-2.2.4.js"
],

在ts文件中使用先定義$爲「接口」,這樣就能夠使用$npm

declare var $: any;

方法2:
npm安裝插件,命令以下:json

npm install jquery --save

例如app.component.ts須要使用,直接importapp

import * as $ from "jquery";

2、angular4引入基於jQuery開發的jQuery的插件
找到文件.angular-cli.json,添加代碼以下jquery插件

"scripts": [
    "assets/jquery-2.2.4.js",
    "assets/miniMobile/miniMobile.js"
],

或者直接在index.html中引入jQuery和插件也行。
以後在ts文件中使用先定義$爲「接口」,這時$已經被插件拓展了$的原型angular4

declare var $: any;

這樣定義之後,jQuery和jQuery插件裏的函數都是能夠使用的,原理很簡單,jQuery文件中的函數自己就是對$原型作開發,jquery插件也是同樣,因此無論什麼文件,只要引入順序正確,再定義上接口就能夠使用了。函數

相關文章
相關標籤/搜索