2016-11-29 127 views
2

我試圖用料設計精簡版,但收到以下錯誤顯示與紋波按鈕:

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

HTML文件:

<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> 
     `; 
    } 

} 

基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'; 
    } 
} 
+0

它描述你的jquery沒有導入你的文件 –

+0

@SanjayPatel是我的語法錯誤還是nythng其他? –

+0

當你使用console.log($)'時,你會得到什麼? –

回答

2

在jQuery自身附加到全局對象,你應該使用import 'jquery'

使用import $ from 'jquery'陰影$'jquery'默認的導出全局對象,但jQuery的不出口任何東西,所以$ === undefined

相關問題