当我尝试从地址栏打开Angular 2路由时,我不断收到http 404响应,但是使用带有“routerLink”指令的锚点导航到路由时没有问题。我试图按照Angular's Deployment Guide中的说明修改我的web.config文件以进行网址重写,但这通常会破坏我的路由。最后,我在我的index.html文件中添加了<base href="/">
,但这也没有帮助。非常感谢社区的任何帮助!
这是我的路由组件代码:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ClientsComponent } from './clients.component';
const routes: Routes = [
{ path: 'clients', component: ClientsComponent },
//^This does not work from address bar, only from routerLinks.
{
path: '**',
redirectTo: '',
pathMatch: 'full'
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
答案 0 :(得分:2)
尝试将usehash设置为true,这可以解决您的问题。
RouterModule.forRoot(routes, { useHash: true })