1

我正在尝试在 url 中传递语言代码,localhost4200/en-us/并在子组件中获取语言代码,但它不起作用,当我尝试localhost4200/en-us/像这样导航时,我的应用程序开始从localhost4200/en-us/assets/img.png. 这是一个使用 Angular 制作的简单单页网站。

应用路由模块

{ path: '', component: LayoutComponent, loadChildren: './pages/pages.module#PagesModule' }, 
{ path: ':locale', redirectTo: '', pathMatch: 'full' }, // for language param
{ path: 'login', component: LoginComponent  },

首页路由模块

{ path: '', component: Index1Component },
//{ path: ':locale', redirectTo: '', pathMatch: 'full' }, // tried here also

在 Index1Component 中获取路由器参数

this._route.params.subscribe(
    (param: any) => {
         let language = param['locale'];
         console.log(language); //it always returns `undefined`
     });

如果我导航到localhost4200/en-us/应用程序没有按预期工作并且它开始请求资源localhost4200/en-us/assets

我还在学习 Angular 路由,我在这里做错了什么?有没有办法设置这个动态语言参数,我希望它是可选的localhost4200/,并且localhost4200/en-us.

4

0 回答 0