簡體   English   中英

AngularJS應用程序中的資源腳本應該是相對的還是絕對的?

[英]Should resource scripts in an AngularJS app be relative or absolute?

我正在構建一個AngularJS單頁面javascript應用程序,因此index.html看起來像:

<html>
<head>
<base href="/" />
...
</head>
<body id="ng-app" ng-app="myAngularApp">
    <div class="holder">
      //templates from app.js are rendered here
    </div>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="bower_components/angular-resource/angular-resource.js"></script>

<script src="scripts/app.js"></script>
</body>
</html>

我在HTML5mode中運行此應用程序,並且遇到了舊版瀏覽器的后備問題。 每當我在IE8中瀏覽到$APPURL$/login這樣的位置時,它會正確地重寫為$APPURL$/#!/login ,但是當我執行嵌套的$APPURL$/locations/my/home它什么都不做(甚至沒有bootstrap)因為它找不到這個特定路徑中的scipts。 我通過將refs設置為我的腳本絕對(使用/作為前綴)修復了這個問題,但我查看的Angular Apps的所有示例都使用了相似的路徑,就像在我的示例中一樣。

我使用NGINX提供我的應用程序,並從眾多示例中獲得以下配置:

location / {
    try_files $uri $uri/ /index.html;
    root /home/web/appdir;
}

使用絕對路徑有什么缺點嗎? 另外,為什么這有必要? 我真的需要傳統的瀏覽器支持,但想繼續使用HTML5mode .....

IE8的問題在於<base href="/" /> -tag,它是一個相對URI,因此 IE 忽略

所以基本上,每當你使用上面的NGINX配置瀏覽到http://my.app.io/where/does/this/go時,它將重定向到原始的http://my.app.io/index.html URI被送到index.html 然后,IE將嘗試解析相對於原始URI的所有資源(腳本,圖像和樣式表),並且無法找到它們,從而破壞了應用程序。

現在,當我們使用像<base href="http://my.app.io/" />這樣的絕對URI重新配置base -tag時,這個問題將不再存在,因為IE將找到與此基URI相關的所有資源。

感謝: https//github.com/yeoman/generator-angular/issues/433https://stackoverflow.com/a/1889957/2818703

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM