2016-05-02 45 views
1

我創建了一個小的角度2示例應用程序,我可以在其中演示路由問題。我定義了以下路線:角度2:直接調用URL不起作用

@RouteConfig([ 
    { path: "/login", name: "Login", component: Login }, 
    { path: "/sites/site1", name: "Site1", component: Site1 } 
]) 

導航到站點1底下的指令工作:

this.router.navigate(["Site1", {}]); 

如果我打電話直接從瀏覽器的網站不工作。在我的情況下http://127.0.0.1:8080/static/sites/site1。但直接調用登錄網站的作品:http://127.0.0.1:8080/static/login

注意:在這種情況下'/靜態'是我的基礎href。

看起來問題只發生在路徑上,其中路徑比一層更深。基礎href工作後的第一個站點(例如登錄)。呼叫更深的站點不起作用(例如站點/站點)。

問題可能與下列應用轉載: https://github.com/lexon0011/IssueDemonstrator

  • 克隆庫,安裝節點模塊,並啓動應用程序(請參見開始)
  • 單擊「查看網站1」:你可以看到路由的工作原理
  • 直接調用的網址:您可以看到路由不工作

任何想法?

謝謝!

+0

貌似http://stackoverflow.com/questions/35137573/angular-2-router-es5-doesnt-work-on-page-reload/35137712#35137712 –

回答

0

我認爲你的問題位於HTML頁面上。你有錯誤的路徑配置。嘗試改變indexDev.html方式如下:

<head> 
    <meta charset="UTF-8"> 
    <title>Issue Demostrator</title> 
    <base href="/static/" /> 
    <!-- Bootstrap stuff --> 
    <script src="/node_modules/jquery/dist/jquery.min.js"></script> 
    <script src="/node_modules/bootstrap/dist/js/bootstrap.min.js"></script> 
    <link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.min.css" /> 

    <!-- Angular2 stuff --> 
    <script src="/node_modules/systemjs/dist/system.src.js"></script> 
    <script src="/node_modules/angular2/bundles/angular2-polyfills.js"></script> 
    <script src="/node_modules/angular2/bundles/angular2.dev.js"></script> 
    <script src="/node_modules/angular2/bundles/router.dev.js"></script> 
    <script src="/node_modules/angular2/bundles/http.dev.js"></script> 
    <script src="app/systemConfig.js"></script> 
    <script type="text/javascript"> 
     System 
      .import("appbuild") 
      .catch(console.log.bind(console)); 
    </script> 
</head> 

通知我添加基地標籤之前,你的鏈接,並改變了自己的路徑。

我希望它能幫助你

+0

謝謝,它的基本標記它的工作原理。 GnnterZöchbauer:HashLocationStrategy也可以,但我需要PathLocationStrategy,因爲我使用OpenId服務器,它將令牌作爲散列碎片 –

0

我想你想切換到HashLocationStrategy

bootstrap(AppCmp, [ 
    ROUTER_PROVIDERS, 
    provide(LocationStrategy, {useClass: HashLocationStrategy}) 
]); 

默認爲PathLocationStrategy(HTML5 pushState的),這就需要服務器的支持。

+0

'HashLocationStrategy '將'/ pagename'轉換爲'/#/ pagename'。有沒有辦法讓它可以再次導航到'/ pagename'?在Angular 1. *中,你可以設置'html5Mode'並在'.htaccess'中寫幾行以去掉'#'。 – Patrick2607

+0

不,如果你需要這個,你需要爲HTML5 pushState配置服務器。如果它適用於'HashLocationStrategy',那麼你知道這是你問題的原因。 –