我有一個基於此示例的多頁面骨幹網應用:https://github.com/asciidisco/grunt-requirejs/tree/master/examples/multipage-shim,它對基本網址正常工作。當我導航到不再位於域根目錄的頁面時,問題就出現了。多頁應用中的正確網址
的目錄結構如下所示:
scripts
├── app
│ ├── controller
│ │ ├── Base.js
│ │ ├── c1.js
│ │ └── c2.js
│ ├── lib.js
│ ├── main1.js
│ ├── main2.js
│ ├── model
│ │ ├── Base.js
│ │ ├── m1.js
│ │ └── m2.js
├── common.js
├── page1.js
└── page2.js
所以,如如果我瀏覽到http://localhost/
,使用以下腳本標籤一切加載正確:
<script data-main="/scripts/page1" src="/path/to/require.js">
(此負載第1頁,這反過來負荷common.js和main1.js)。
但是,如果我導航到http://localhost/another/url/
,那麼相同的腳本標記會成功加載page1.js和common.js,但是當它嘗試加載main1.js時,我會得到一個404,因爲它是從相對URL加載的。(嘗試加載http://localhost/another/url/scripts/app/main1.js
我baseUrl
設置爲'scripts'
,和我使用的咕嚕(https://github.com/asciidisco/grunt-requirejs)建設
page1.js的內容僅僅是這樣的:
//Load common code that includes config, then load the app logic for this page.
require(['./common'], function (common) {
require(['app/main1']);
});