相关疑难解决方法(0)

Angular 2:从父组件获取RouteParams

如何从父组件获取RouteParams?

App.ts:

@Component({
  ...
})

@RouteConfig([
  {path: '/', component: HomeComponent, as: 'Home'},
  {path: '/:username/...', component: ParentComponent, as: 'Parent'}
])

export class HomeComponent {
  ...
}
Run Code Online (Sandbox Code Playgroud)

然后,在中ParentComponent,我可以轻松获取用户名参数并设置子路由.

Parent.ts:

@Component({
  ...
})

@RouteConfig([
  { path: '/child-1', component: ChildOneComponent, as: 'ChildOne' },
  { path: '/child-2', component: ChildTwoComponent, as: 'ChildTwo' }
])

export class ParentComponent {

  public username: string;

  constructor(
    public params: RouteParams
  ) {
    this.username = params.get('username');
  }

  ...
}
Run Code Online (Sandbox Code Playgroud)

但是,如何在这些子组件中获得相同的"用户名"参数?做与上面相同的技巧,不会这样做.因为这些参数是在ProfileComponent中定义的还是什么?

@Component({
  ...
})

export class ChildOneComponent {

  public …
Run Code Online (Sandbox Code Playgroud)

angular2-routing angular

71
推荐指数
4
解决办法
4万
查看次数

在Angular 2中获取父路由参数(路由器v3.0.0-alpha.6)

展望设置使用的一个网站谷歌的推荐的多区域和多语言网站的做法,利用子目录与gTLD的(例如特别的模型.www.stackoverflow.com/en/questions/ask,www.stackoverflow.com/fr/questions/ask等).

根据顶级子目录,我想设置默认语言并获取相应的JSON数据.进一步解释,当用户直接导航到www.mywebsite.com/en/home时,默认语言将被设置为en,我的内容服务将获取该en.json文件.

在上面的示例中,我的路由将配置如下:

export const routes: RouterConfig = [
  { path: ':country',          component: Home },
  { path: ':country/home',     component: Home },
  { path: ':country/about',    component: About },
  { path: ':country/contact',  component: Contact }
];
Run Code Online (Sandbox Code Playgroud)

我试图找出如何抓住:country路线参数(如果网址是www.mywebsite.com/en/about/我试图让:country路由参数值en)的时候Home,AboutContact组件.这是我尝试过的几件事:

export class Home implements OnInit {
    constructor(
        private route: ActivatedRoute
    ) {}

    private sub: any;

    ngOnInit(){
       this.sub = this.route.params.subscribe(params => {
         let country …
Run Code Online (Sandbox Code Playgroud)

angularjs angular-routing angular2-template

4
推荐指数
1
解决办法
3449
查看次数