How to use jquery in ionic 3

半腔热情 提交于 2019-12-06 05:48:47

问题


I'm trying to load external website in a div using jquery in ionic 3.

TS:

export class HomePage 
{
  constructor(public navCtrl: NavController) {    
      $('#loadExternalURL').load('http://www.google.com');
  }
}

HTML:

<ion-content>
    <div id="loadExternalURL"></div>
</ion-content>

I'm getting blank screen on serving the ionic app. Is there anything I'm missing? Any Suggestion?


回答1:


I did it in following way,

  1. Install Jquery module in your IONIC-3 app,

    npm install jquery --save

  2. Import JQuery in HomePage.ts

    import * as $ from "jquery";

  3. Use $ to call jquery methods.

I wait for method ngAfterViewInit to make sure view is initialized.

ngAfterViewInit(){
    $(document).ready(function(){
        alert('JQuery is working!!');
    });
}



回答2:


To use jQuery in ionic project do it this way:

Install the Jquery module in your IONIC-3 app as follows:

1, npm install jquery --save
2, npm install @types/jquery

then import jquery on your page like this

3, import * as $ from "jquery";

You can then use the jquery $ as you would in normal javascript. I have used it for a jquery ajax call to get remote data like this:

 $.ajax({
    url: "my api url",
    method: "POST", //you can do post, get, put
    data: my-data
}).done(function(res){
     //process response from server
}).fail(function(err){
     //return error message.
});

It worked very fine.




回答3:


You need to import jQuery into your project somehow. If you want to use HTML you need to add it near the top of your page

<script src="[path to jQuery source]"></script>

Or use a .d.ts file defining jQuery

declare module "jquery" {
    export = $;
}
declare var jQuery: JQueryStatic;
declare var $: JQueryStatic;

then define it where needed

<reference path="jquery.d.ts" />

References used:

TypeScript and libraries such as jQuery (with .d.ts files)

How do I get jQuery autocompletion in TypeScript?




回答4:


You not need to use jQuery

You can add iframe

<div>
    <iframe src="yourexternalpage url"></iframe>
</div>


来源:https://stackoverflow.com/questions/46823909/how-to-use-jquery-in-ionic-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!