2016-11-22 159 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问题? –