首页 文章

Angular 4 - 路由器redirectTo使用变量段

提问于
浏览
-1

在下面的路由路由中,我在尝试获取http://localhost:4200/时收到以下错误

错误错误:未捕获(承诺):错误:无法重定向到'/:userCountry /:userLanguage / home' . 找不到':userCountry' . 错误:无法重定向到'/:userCountry /:userLanguage / home' . 找不到':userCountry' . 在ApplyRedirects.webpackJsonp ... / .. / .. / router / @ angular / root.es5.js.ApplyRedirects.findPosParam(router.es5.js:1784)

每个其他路由路径都在工作,它只是redirectTo似乎没有替换变量userCountry(和userLanguage也)的值 .

任何帮助将非常感激 .

const appRoutes: Routes = [
   { path: '', redirectTo: '/:userCountry/:userLanguage/home', pathMatch: 'full' },
   { path: ':userCountry/:userLanguage/home', component: HomeComponent },
   { path: ':userCountry/:userLanguage/about', loadChildren: './about/about.module#AboutModule' },
   { path: ':userCountry/:userLanguage/terms', loadChildren: './terms/terms.module#TermsModule' },
   { path: ':userCountry/:userLanguage/privacy', loadChildren: './privacy/privacy.module#PrivacyModule' },
   { path: '**', component: PageNotFoundComponent }
];

当错误附加时,我的变量已经有了值 .

这是我的console.log()的摘录

[AppRoutingModule.constructor]this.userLanguage=fr
    [AppRoutingModule.constructor]this.userCountry=ca  
    ...  
    ERROR Error: Uncaught (in promise): Error: Cannot redirect to '/:userCountry/:userLanguage/home'. Cannot find ':userCountry'

1 回答

  • 1

    当它在 '' 路由中时,您需要将默认值传递给 userCountryuserLanguage 以进行路由匹配 . 你可以这样做

    const appRoutes: Routes = [
       { path: '', redirectTo: '/germany/ge/home', pathMatch: 'full' },
       { path: ':userCountry/:userLanguage/home', component: HomeComponent }
       ...
    }
    

相关问题