在ng2中使用jquery

在Angular2中使用jquery方法:
首先在index.html中引入jquery文件

<script src="https://cdn.bootcss.com/jquery/2.1.3/jquery.js"></script>
接下来定义使用它:

import {Component , ElementRef,OnInit} from '@angular/core';

declare var jQuery:any; //定义jquery

@Component({
template :`
<button>点击</button>

})

export class TestComponent implements OnInit {
constructor(public _elementRef: ElementRef){}

ngOnInit():any{
jQuery(this._elementRef.nativeElement).on('click',function(){
console.log(123);
})
}

}

jQuery修改为$符号也可以的

https://stackoverflow.com/questions/30623825/how-to-use-jquery-with-angular
posted @ 2017-12-16 00:36  天降之水  阅读(32)  评论(0)    收藏  举报