【问题标题】:Angular2 getting string value from a route childAngular2从路由孩子获取字符串值
【发布时间】:2016-04-27 20:20:56
【问题描述】:


我需要从子路由视图设置标题标题...
我有父组件:

    @Component({
        selector: 'parent-component',
        template: `
<header>{{headerTitle}}</header>
<router-outlet></router-outlet>
`,
        directives: [ROUTER_DIRECTIVES]
    })
    @RouteConfig([
        { path: '/link1', name: 'Link1', component: Link1Component, useAsDefault: true },
        { path: '/link2', name: 'Link2', component: Link2Component },
    ])

    export class ParentComponent {

        public sharedHeaderTitle: string;
        constructor(public router: Router) { }

    }

有一个子路由标签:

@Component({
    template: '<div></div>'
})

export class Link1Component {
    public headerTitle: string;
    constructor() {
        this.headerTitle = "Link1 page";
    }
}

还有一个

@Component({
    template: '<div></div>'
})

export class Link2Component {
    public headerTitle: string;
    constructor() {
        this.headerTitle = "Link2 page";
    }
}

我找到了here,但它不能用于路由...
最好的制作方法是什么?


已解决

我找到了一种解决方案,认为这是最好的方法:
http://plnkr.co/edit/LYTXmY9Fwm8LwumICWDT

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    Link1ComponentLink2Component 需要与ParentComponent 通信。由于它们被限制在路由器插座中,我不确定这是否可能除了订阅路由器事件(请参阅How to detect a route change in Angular 2?

    我还会考虑对设计进行一些更改,并将设置标题的责任转移到ParentComponent,即ParentComponent 在发出router.navigate(['LinkXComponent'] 命令之前设置标题。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2017-06-03
      • 2016-12-08
      • 2013-06-27
      • 1970-01-01
      • 2017-07-20
      • 2018-02-15
      • 2017-03-28
      • 2014-01-24
      • 1970-01-01
      相关资源
      最近更新 更多