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

javascript - Uncaught (in promise) TypeError: $ is not a function - Stack Overflow

programmeradmin2浏览0评论

I am trying to display a button with ripple using material design lite but getting the following error:

app.js:3 Uncaught (in promise) TypeError: $ is not a function(…)

html file:

  <body>
      <script>
System.paths['jquery'] = './node_modules/jquery/dist/jquery.js';
              System.import('src/app.js');
</script> 
  </body>

app.js:

      import $ from 'jquery';
import {Button} from './ui/button.js';
let b=new Button('click me');
b.appendToElement($('body'));

button.js :

      import {BaseElement} from './base-element.js';

export class Button extends BaseElement {

    constructor(title) {
        super();
        this.title = title;
}

    getElementString() {
        return `
            <button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent"
                style="">
                ${this.title}
            </button>
        `;
    }

}

base-element.js:

    import $ from 'jquery';

export class BaseElement {

    constructor() {
        this.element = null;  // jQuery object
    }

    appendToElement(el) {
        this.createElement();
        el.append(this.element);
}

    createElement() {
        let s = this.getElementString();
        this.element = $(s);
    }

    getElementString() {
        throw 'Please override getElementString() in BaseElement';
    }
}

I am trying to display a button with ripple using material design lite but getting the following error:

app.js:3 Uncaught (in promise) TypeError: $ is not a function(…)

html file:

  <body>
      <script>
System.paths['jquery'] = './node_modules/jquery/dist/jquery.js';
              System.import('src/app.js');
</script> 
  </body>

app.js:

      import $ from 'jquery';
import {Button} from './ui/button.js';
let b=new Button('click me');
b.appendToElement($('body'));

button.js :

      import {BaseElement} from './base-element.js';

export class Button extends BaseElement {

    constructor(title) {
        super();
        this.title = title;
}

    getElementString() {
        return `
            <button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent"
                style="">
                ${this.title}
            </button>
        `;
    }

}

base-element.js:

    import $ from 'jquery';

export class BaseElement {

    constructor() {
        this.element = null;  // jQuery object
    }

    appendToElement(el) {
        this.createElement();
        el.append(this.element);
}

    createElement() {
        let s = this.getElementString();
        this.element = $(s);
    }

    getElementString() {
        throw 'Please override getElementString() in BaseElement';
    }
}
Share Improve this question asked Nov 29, 2016 at 7:54 YaSh ChaudharyYaSh Chaudhary 2,7259 gold badges39 silver badges76 bronze badges 8
  • it describe that your jquery is not imported in your file – Sanjay Commented Nov 29, 2016 at 7:58
  • @SanjayPatel is my syntax wrong or nythng else? – YaSh Chaudhary Commented Nov 29, 2016 at 8:02
  • What do you get when you console.log($)? – Michał Perłakowski Commented Nov 29, 2016 at 8:37
  • @Gothdo same error – YaSh Chaudhary Commented Nov 29, 2016 at 8:43
  • 2 Try import 'jquery'? – sdgluck Commented Nov 29, 2016 at 10:07
 |  Show 3 more ments

1 Answer 1

Reset to default 3

As jQuery attaches itself to the global object you should use import 'jquery'.

Using import $ from 'jquery' shadows $ on the global object with the default export of 'jquery', but jQuery doesn't export anything, so $ === undefined.

发布评论

评论列表(0)

  1. 暂无评论