2016-11-22 202 views
2

我使用路由角色2與打字稿。路徑:「**」未找到頁面

在主index.html我添加<base href="">,不<base href="/">,因爲我需要爲我的項目的特殊路線,一切工作正常,但我有一些問題,找不到網頁。這裏是我的app.route.ts的一部分:

const appRoutes: Routes = [ 
    { component: LaskComponent, path: "table_per" }, 
    { component: LaskComponent, path: "table_per/:id" }, 
    { component: DashboardComponent, path: "dashboard" }, 
    { component: LoginComponent, path: "login" }, 
    { path: "", pathMatch: "full", redirectTo: "login" }, 
    { component: HomeComponent, path: "home" }, 
    { component: NotFoundComponent, path: "not_found" }, 
    { path: "**", redirectTo: "not_found" }, 
]; 
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes); 

這是NotFoundComponent

import { Component } from "@angular/core"; 
@Component({ 
    template: ' 
     <div class="container"> 
      <h1>Not found page</h1> 
     </div>', 
}) 
export class NotFoundComponent {}; 

當我開始localhost在Chrome瀏覽器項目,我重定向到localhost/login,一切正常,但是當我檢查一個沒有找到的頁面,例如localhost/sxvknb,我得到這個頁面localhost/sxvknb/login並再次以登錄形式重定向。問題是什麼?也許有

{ path: "**", redirectTo: "not_found" } 

啓動項目中,我可以用3開始部署:

enter image description here

+0

可以提供'APP_BASE_HREF'而不是''http://stackoverflow.com/q uestions/34535163/angular-2-router-no-base-href-set/34535256#34535256 –

回答

1

這可能會解決你的問題:

import {APP_BASE_HREF} from '@angular/common'; 

@NgModule({ 
    declarations: [AppComponent], 
    bootstrap: [AppComponent], 
    imports: [BrowserModule, routing /* or RouterModule */], 
    providers: [{provide: APP_BASE_HREF, useValue : '/' }] 
]); 

參見Angular 2 router no base href set

+0

男人,非常感謝! –

+0

不客氣。很高興聽到這個解決了它。 –

+0

對不起,你可以告訴我,當我在'localhost'中使用我的應用時,它的工作很好,但是當我從服務器啓動我的應用時:'10.10.0.1/object/go'我從組件中找不到一個頁面,whats問題? –