我在角度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个部署开始:
答案 0 :(得分:1)
这可能会解决您的问题:
import {APP_BASE_HREF} from '@angular/common';
@NgModule({
declarations: [AppComponent],
bootstrap: [AppComponent],
imports: [BrowserModule, routing /* or RouterModule */],
providers: [{provide: APP_BASE_HREF, useValue : '/' }]
]);