2016-11-21 13 views
0

SystemJSを使用してブラウザに簡単なjavascriptファイルをロードする方法を教えてもらえますか?systemjs - Loading jquery

packages: { 
     app: { 
     main: './js/main.js', 
     defaultExtension: 'js' 
     }, 
     rxjs: { 
     defaultExtension: 'js' 
     }, 
     'jquery': { 
     main: 'node_modules/jquery/dist/jquery.min.js' 
    } 
    } 

をしかし、私は私のブラウザで次の行を書くたび

$(document).ready(function(){alert("Hello");}); 

マイ全体SystemJS構成は次のとおりです。私は私のnode_modulesからのjQueryをロードしようとしています

次syntax-を使用してフォルダ - :

/** 
* System configuration for Angular samples 
* Adjust as necessary for your application needs. 
*/ 
(function (global) { 
    System.config({ 
    paths: { 
     // paths serve as alias 
     'npm:': 'node_modules/' 
    }, 
    // map tells the System loader where to look for things 
    map: { 
     // our app is within the app folder 
     app: 'app', 
     // angular bundles 
     '@angular/core': 'npm:@angular/core/bundles/core.umd.js', 
     '@angular/common': 'npm:@angular/common/bundles/common.umd.js', 
     '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js', 
     '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js', 
     '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', 
     '@angular/http': 'npm:@angular/http/bundles/http.umd.js', 
     '@angular/router': 'npm:@angular/router/bundles/router.umd.js', 
     '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js', 
     '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js', 
     // other libraries 
     'jquery':     'npm:jquery/dist/jquery.min.js',  
     'rxjs':      'npm:rxjs', 
     'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js' 
    }, 
    // packages tells the System loader how to load when no filename and/or no extension 
    packages: { 
     app: { 
     main: './js/main.js', 
     defaultExtension: 'js' 
     }, 
     rxjs: { 
     defaultExtension: 'js' 
     } 
    } 
    }); 
})(this); 

$が定義されていないというエラーが表示されます。

SystemJSを使用してシンプルな.jsファイルをブラウザにロードするにはどうすればよいですか?

+0

'$'の代わりに 'jQuery'を使ってみましたか? – jkris

+0

@jkris Yupと同じ効果があります。 jQueryは定義されていません。 –

答えて

1

jQueryは環境にやさしいライブラリであり、適切なローダーが利用可能であれば、ブラウザー内でグローバルネームスペースwindowに無条件で自分自身を挿入しないため、この動作は正しく、予期されます。 SystemJSは、jQueryがAMDモジュールであることを検出し、AMD define APIを使用して正しく登録するための正しいラッパーを提供します。

これは、使用するためにインポートする必要があることを意味します。ブラウザコンソールでそれにアクセスしたい場合は

次のコードは、活字体とバベル

import $ from 'juery'; 

$(document).ready(() => { 
    alert("Hello"); 
}); 

の下で動作します。次のようにすることができます

// In your browser tools console 
SystemJS.import('jquery').then(({default}) => { 
    window.$ = default; 
});