当前位置:  开发笔记 > 编程语言 > 正文

如何在Angular2中使组件可以普遍访问

如何解决《如何在Angular2中使组件可以普遍访问》经验,为你挑选了1个好方法。

我基本上想要创建一个自定义对话框组件,我可以在我的Angular2应用程序中的任何位置使用它,无论应用程序树中的using组件位于何处.为简单起见,我们称之为SayHello组件.

考虑以下应用程序树:在此输入图像描述

所以,假设我想要SomeComponent.level3.component来调用SayHello.component中的对话框.

在Angular 1.x中,我会将RootScope注入控制器并以此方式点亮对话框.现在,我了解(或多或少)对于Angular2,你可以在组件树上冒泡事件(使用事件发射器),但是从树上的SomeComponent.level3.component向下到SayHello一直冒泡一个事件似乎很乏味. .零件.

所以我想我会创建一个SayHello服务,我会在任何想要点亮对话框的地方注入.这是我制定的代码草图.

myApp.component.ts

import {SayHelloComponent} from "<>/sayHello.component";
import {BunchOfComponents} from "<>/bunchOfComponents";

@Component({
    directives: [SayHelloComponent],
    selector: "my-app",
    templateUrl: `Within this component exists
                      SomeComponent.level3.component 
                      
                      `

})
export class myAppComponent {
    showDialog = false;
    message = "";

    constructor(private sayHelloService: SayHelloService) {
        this.showDialog = sayHelloService.showDialog;
        this.message = sayHelloService.message;

    }
}

SayHelloService.ts

import {Injectable} from 'angular2/core';

@Injectable()
export class SayHelloService {
    public showDialog: boolean = false;
    public message: string ="";

    constructor() {

    }

}

SayHello.component.ts

import {Component} from "angular2/core";
import {SayHelloService} from "<>/SayHelloService";
@Component({
    directives: [],
    selector: "say-hello",
    template: "[do hello component]"
})
export class SayHelloComponent {
    @Input() showdialog: boolean;
    @Input() message: string;

       constructor(private sayHelloService: SayHelloService) {

    }
    //This idea here is to detect change in showDialog
    //If true then do an alert with the message
    ngOnChanges(changes: { [propName: string]: SimpleChange }) {
        var obj = changes["showdialog"];
        if (obj !== null) {
            if (changes["showdialog"].currentValue === true) {
                alert(this.message);
                this.sayHelloService.showDialog = false;
            }

        };
    }

}

SomeComponent.level3.component

import {Component} from "angular2/core";
import {SayHelloService} from "<>/SayelloService";

@Component({
    directives: [],
    selector: "some-component",
    template: ""
})
export class PageContactUsComponent {

    constructor(private sayHelloService: SayHelloService) {

    }


    doHello(): void {
        this.sayHelloService.message = "Hello world";
        this.sayHelloService.showDialog = true;
    }
}

appBoot.ts

import {bootstrap} from "angular2/platform/browser";
import {MyAppComponent} from "</MyAppComponent";
import {SayHelloService} from "<>/SayHelloService";

bootstrap(MyAppComponent, [
    SayHelloService
]);

不用说,这不起作用.我没有得到任何错误,但SayHello.component没有检测到'showdialog'值的任何变化......所以没有任何反应.关于如何正确地做到这一点的任何想法将非常感激.



1> Mark Rajcok..:

如上面的评论所述,

在服务中放置一个Observable(注意,不是EventEmitter)

在其他组件可以调用的服务上放置showDialog()API /方法.showDialog()方法应调用next()来发送事件.

您的对话框组件可以订阅该事件,并在收到事件时取消隐藏/显示.

要在服务中包装Observable,请参阅此答案.

推荐阅读
TXCWB_523
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有