How can I use jQuery with Ionic 2?

前端 未结 1 1414
渐次进展
渐次进展 2020-12-29 08:09

I have been given some JQuery code that I need to use with my ionic 2 project. How can I include it and the JQuery library?

The code looks like this (this is just a

相关标签:
1条回答
  • 2020-12-29 08:40

    Updated at: 12/04/2018

    1. First of all, install jQuery in your ionic project:

    $ npm install jquery --save

    2. After that, install jQuery global derectory to typings (so you can import it):

    $ npm install @types/jquery
    

    3. Then, you can import JQuery in the page/component where you want to use it (for example: home.ts), with the following code:

    import * as $ from 'jquery'

    And that's all, now it should be working.


    Checking:

    To check if it worked , you can simply try the following:

    1. In your component/page (for example: home.html) add an element with an specific id (in this case: myButton), and a method:

    <button id="myButton" (click)="changeTextColor()">Click Me!</button>

    2. In your component/page: (in this case: home.ts) add the method:

    changeColor(){ $('#myButton').text('white'); }

    And that should change the text of the button.

    0 讨论(0)
提交回复
热议问题