Angular-如何在Angular2中使用jQuery及其插件


搭建好環境后

一:首先在index.html中引用jquery,就像我們以前做的那樣

    

二:然后我們編寫我們的app.component.ts

import { Component,OnInit} from '@angular/core';
declare var $:any;
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent  implements OnInit{
  ngOnInit()
  {
    $("#title").html("<p>this is a string from jQuery html setting</p>");
  }
}

  首先需要使用declare生命我們的jQuery,使之成為一個可用的變量,然后,我們需要導入OnInit模塊並實現,我們編寫的jquery代碼就在這里,文中展示了我們向id為title的標簽替換內容,HTML頁面是這樣的

<div id="title" class="title">
</div>

  這就意味着我們可以在Angular2中正常使用jQuery了

 

jquery插件引用:

  接下來做個簡單的jQuery插件使用的例子,首先找一個我們要使用的jQuery插件,我用的是這個,3D視覺圖片

  一:首先在index.html 中引用

  

  二:然后在我們剛才的app.component.ts中的ngOnInit中寫入以下初始化插件代碼

ngOnInit()
  {
    $(".card").faceCursor({}); 
    $("#title").html("<p>this is a string from jQuery html setting</p>");
  }

    然后我們編寫html,css,運行就可以了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM