当我尝试从地址栏打开Angular 2路由时,我不断收到http 404响应,但是使用带有“routerLink”指令的锚点导航到路由的工作没有问题。我试图按照Angular's Deployment Guide中的说明修改我的web.config文件以进行url重写,但这通常会完全破坏我的路由。最后,我在我的index.html文件中包含了<base href="/">
,但这也没有帮助。我们非常感谢社区的任何帮助!Angular 2路由 - 从地址栏打开链接
这里是我的路由组件代码:
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 { }
您可以轻松地说解决了使''#基于路由这个问题'[RouterModule.forRoot(路线,{useHash:真})]'如果你不需要'html5mode'路由 –
谢谢Pankaj,这个工作,但我不希望在我的网站上有#符号。看起来像一个“哈克”解决方法。这是Angular 2和IIS的一个已知问题吗? – DotNetDev
不,这可以发生在任何服务器..不专门与IIS。你必须配置你的服务器来处理这样的请求..我可以知道你使用'systemjs' /'weback'的模块加载器吗? –