Angular 使用角度4刷新将重复当前路线

Angular 使用角度4刷新将重复当前路线,angular,angular-router,angular4-router,Angular,Angular Router,Angular4 Router,我正在一个项目中使用Angular 4。我对Angular没有太多经验,我应该修复一个bug。我认为这很简单,但是当我加载应用程序并获得/home路径时,如果我单击刷新按钮,另一个/home将附加到URL 我的RouterModuleconfig如下所示: RouterModule.forRoot([ { path: '', redirectTo: 'home', pathMatch: 'full' }, { path: 'home', component: HomeCompon

我正在一个项目中使用Angular 4。我对Angular没有太多经验,我应该修复一个bug。我认为这很简单,但是当我加载应用程序并获得
/home
路径时,如果我单击刷新按钮,另一个
/home
将附加到URL

我的
RouterModule
config如下所示:

RouterModule.forRoot([
    { path: '', redirectTo: 'home', pathMatch: 'full' },
    { path: 'home', component: HomeComponent },
    /* other paths mapping to components */
    { path: '**', redirectTo: 'home' }
])

新手有什么想法吗?

您需要从使用字符串改为使用组件名称

RouterModule.forRoot([
    { path: '', redirectTo: HomeComponent , pathMatch: 'full' },
    { path: 'home', component: HomeComponent },
    /* other paths mapping to components */
    { path: '**', redirectTo: 'home' }
])

我无法用计算机再现这一点。下面的gif显示了您的配置如何按照我的预期工作:

启用跟踪
并发布结果,以便我能为您提供更多帮助:

RouterModule.forRoot(
  appRoutes,
  { enableTracing: true } // <-- insert this here
)
RouterModule.forRoot(
批准,

{enableTracing:true}//事实上,我试过了,它很有效。但是为什么?为什么我不能将
'
重定向到
'home'
并重用它的组件定义?不行,pathMatch:“full”表示整个URL路径需要匹配,并由路由匹配算法使用。使用组件名称代替我们可以在redirectTo属性中使用路径,而不是component.@Tom是正确的,谢谢你的帮助!我尝试过启用跟踪功能,但它似乎仍然运行在相同的路径上,即使URL是
home/home
routesRecognited(id:1,URL:'/',urlAfterRedirects:'/home',state:Route(URL:'',path:''){Route(URL:'home',path:'home'))
事实上,我认为这是
造成的。根据Angular docs,我将其更改为
,并且删除了第二个
标记,现在不自觉的重复消失了。这可能是罪魁祸首吗?@Viktor我也有这种怀疑。很可能是。尝试使用base href和不使用base href,如果是c因为然后写下这个问题的答案,这样其他有这个问题的人就不会感受到你的痛苦。