最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using jQuery with Aurelia - Stack Overflow

programmeradmin2浏览0评论

I have an Aurelia app and in app.js I want to use jQuery.

So my config.js looks like:

System.config({
...
  map: {
    ...
    "jquery": "npm:[email protected]",
    ...
   }
}

And in app.js I import the jQuery like this:

var $ = require('jquery');

But when I require it, I get the blank site. Same it was with import:

import $ from 'jquery';

What is wrong?

Thanks

EDIT: ok, solved. The problem is, the jQuery code must be called inside the attached() mehtod. So like this:

export class Class1 {
    attached() {
        //jQuery code here
    }
}

I have an Aurelia app and in app.js I want to use jQuery.

So my config.js looks like:

System.config({
...
  map: {
    ...
    "jquery": "npm:[email protected]",
    ...
   }
}

And in app.js I import the jQuery like this:

var $ = require('jquery');

But when I require it, I get the blank site. Same it was with import:

import $ from 'jquery';

What is wrong?

Thanks

EDIT: ok, solved. The problem is, the jQuery code must be called inside the attached() mehtod. So like this:

export class Class1 {
    attached() {
        //jQuery code here
    }
}
Share Improve this question edited Jan 15, 2016 at 12:45 o..o asked Jan 13, 2016 at 15:54 o..oo..o 1,9217 gold badges34 silver badges65 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 9

You need to install jquery from https://github.com/components/jquery

But if you use aurelia-skeleton, you can import it from bootstrap

import 'bootstrap';

and then use $ everywhere in app, or

import $ from 'bootstrap'

Same is for jqueryui. If needed get it from https://github.com/components/jqueryui

Just to be different! We use jQuery and I tried adding it via config.js and using import etc - which worked ok. But we also use a couple of js libraries that we load using a script tag in the main html page and these require jquery. I tried loading them using import too but they weren't really designed for it and it just got too complicated so in the end we just made life very simple:

script tag for jquery in the main html page
script tag from 3rd party js libraries in the main html page

job done!

It also has a potential benefit of being able to load both jquery and the libraries from a CDN should you wish to.

Perhaps lose the benefits of loading modules via import but we use both jquery and the other libs throughout the app so we're not really losing out plus I don't have to remember to import them when I create a new module :-)

jquery is installed in \jspm_packages\github\components (at least in my case). If this is your case you should use:

System.config({
...
  map: {
    ...
    "jquery": "github:components/[email protected]",
    ...
   }
}

Link to Example in plunker.

npm install jquery --save

and add to dependencies section of vendor-bundle in aurelia.json:

{
     "name": "jquery",
     "path": "../node_modules/jquery/dist",
     "main": "jquery.min.js",
     "exports": "$",
     "map": "jquery.min.map"
}

Then you can use $ in your code for jQuery as usual.

发布评论

评论列表(0)

  1. 暂无评论