如果您在head標籤寫這樣的腳本,它不是同步的瀏覽器加載。與瀏覽器中的app.js相比,main.js花了很長時間加載。因此你得到這個錯誤。嘗試通過main.js文件加載app.js
您必須使用shim : {}
來定義requireJS配置文件中的依賴項。
main.js
require.config({
paths: {
'angular': '../lib/angular/angular',
'angular-route': '../lib/angular-route/angular-route',
'domReady': '../lib/requirejs-domready/domReady'
},
/**
* for libs that either do not support AMD out of the box, or
* require some fine tuning to dependency mgt'
*/
shim: {
'angular': {
exports: 'angular'
},
'angular-route': {
deps: ['angular']
}
},
deps: [
// kick start application... see bootstrap.js
'./bootstrap'
]
});
在你必須引導你的角度應用,然後main.js必須從bootstrap.js叫app.js
bootstrap.js
define([
'require',
'angular',
'app',
'routes'
], function (require, ng) {
'use strict';
/*
* place operations that need to initialize prior to app start here
* using the `run` function on the top-level module
*/
require(['domReady!'], function (document) {
ng.bootstrap(document, ['app']);
});
});
這裏在引導文件中,你的app.js被調用,所以你總是在main.js之後調用app.js
有關使用requireJS進行角度轉換的更多信息,請參閱startersquad示例。
閱讀documentation瞭解更多信息。
請參閱http://stackoverflow.com/questions/13225245/require-js-synchronous – Shohel
我是第一次加載它,因此我無法在鏈接共享中使用該建議 – Santhosh
您應該有'data-main =「 scripts/app「'和'app.js'應該寫成require.js模塊。 – Quentin