角度 2 路由 - 从地址栏打开链接



当我尝试从地址栏打开 Angular 2 路由时,我不断收到 http 404 响应,但是使用带有"routerLink"指令的锚点导航到路由没有问题。 我试图按照 Angular 部署指南中的说明修改我的 web.config 文件以进行 url 重写,但这通常会完全破坏我的路由。 最后,我确实在我的索引.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 { }

尝试将 usehash 设置为 true,这应该可以解决您的问题。

RouterModule.forRoot(routes, { useHash: true })

最新更新