【问题标题】:Pass a component from child to a parent directive in angular 2以角度 2 将组件从子组件传递给父指令
【发布时间】:2016-06-03 12:40:14
【问题描述】:

这是我的父组件:

@Component({
    selector : "app",
    template : '        
            <app-header></app-header>
            <top-navigation></top-navigation>
            <router-outlet></router-outlet>    
            <app-footer></app-footer>                
            <page-js-rersources></page-js-rersources>',
    directives : [AppHeader, AppFooter]
})
@RouteConfig([
    {path: '/', name: "UserHome", component: LandingPage, useAsDefault: true },
    {path: '/login', name: "Login", component: LoginSignUp },
    {path: '/splash', name: "SplashBuildup", component: Splash },
    {path: '/home', name: "HomeBuildup", component: Home }        
])
export class UserLayoutBuildUp{

    constructor(){

    }

}

这是我的子组件:

@Component({
    templateUrl: "splash.tmpl.htm",
})
export class Splash{

}

这是我的顶级导航组件:

@Component({
    selector: "top-navigation",
    templateUrl: "topNavigation.tmpl.htm"
})
export class TopNavigation{

}

我想在 UserLayoutBuildUp 组件的 top-navigation 选择器中包含我的顶部导航组件。

我已经尝试过 Angular 2 文档,但无法弄清楚有关将组件注入顶级选择器的任何信息。

【问题讨论】:

  • 您是否尝试过使用可以在UserLayoutBuildUpSplash 中注入的服务?
  • 不,我没有。 UserLayoutBuildUp 是我的引导组件,我希望我的 topNavigation 组件在任何路径都处于活动状态时处于活动状态
  • 你能提供一些构建相同的东西吗
  • 我确实为你提供了一些东西:)

标签: angular angular2-routing angular2-directives


【解决方案1】:

一种方法是使用您在bootstrap 注入的服务。然后使用路由器生命周期钩子来控制这个服务。这将导致如下结果:

前面有未经测试的代码..

配置服务

export class ConfigurationService { //or whatever name you'd like

    public showTopNavigation: boolean = false;  
    //... use it for other settings you might come across
}

引导

bootstrap(AppComponent, [ConfigurationService]); //And other things

UserLayoutBuildUp

@Component({
    selector : "app",
    template : `        
            <app-header></app-header>
            <top-navigation *ngIf="_configuration.showTopNavigation"></top-navigation>
            <router-outlet></router-outlet>    
            <app-footer></app-footer>                
            <page-js-rersources></page-js-rersources>`,
    directives : [AppHeader, AppFooter]
})
@RouteConfig([
    {path: '/', name: "UserHome", component: LandingPage, useAsDefault: true },
    {path: '/login', name: "Login", component: LoginSignUp },
    {path: '/splash', name: "SplashBuildup", component: Splash },
    {path: '/home', name: "HomeBuildup", component: Home }        
])
export class UserLayoutBuildUp{

    constructor(private _configuration: ConfigurationService){}

}

SplashComponent

@Component({
    templateUrl: "splash.tmpl.htm",
})
export class SplashComponent {

    constructor(private _configuration: ConfigurationService){}

    routerOnActivate() : void {
       this._configuration.showTopNavigation = true;
    }

    routerOnDeactivate() : void {
       this._configuration.showTopNavigation = false; 
    }
}

【讨论】:

  • 正如您所提到的“一种使用方式”,您是否正在考虑另一种使用方式..???
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-10
  • 2020-08-24
  • 2020-02-06
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 2019-04-02
相关资源
最近更新 更多