简体   繁体   English

如何在 Angular 中的另一个组件中使用变量

[英]How to use a variable from a component in another in Angular

I have two component.我有两个组成部分。 alert-component and master component .警报组件和主组件

Now I want to use master-component variable into alert component.现在我想将 master-component 变量用于警报组件。

But without import it because in master-component I have already import alert-component.但是没有导入它,因为在主组件中我已经导入了警报组件。

And again if I try to import master-component into alert-component then it will throw circular dependency error.再一次,如果我尝试将主组件导入警报组件,那么它会抛出循环依赖错误。

So I want to access variable of master-component into alert-component without import it.所以我想访问 master-component 的变量到 alert-component 而不导入它。

So its possible?那么有可能吗?

You could use an @Input() variable for your alert component and forward it within your master component.您可以为您的警报组件使用 @Input() 变量并将其转发到您的主组件中。 In the Master Component's template, inject the alert component and forward the input with square brackets:在主组件的模板中,注入警报组件并使用方括号转发输入:

// master.component.html
...
    <alert-component [alertInput1]="masterVariable1"></alert-component>
...

In the master component's controller, have the variable defined, that you want to forward to the child component:在主组件的 controller 中,定义了要转发给子组件的变量:

// master.component.ts
{
    ...
    public masterVariable1: string = "test content from master";
    ...
}

In the child component's controller, define the input variable:在子组件的controller中,定义输入变量:

// alert.component.ts
{
    ...
    @Input() alertInput1: string = undefined; // alertInput1 will get the value "test content from master"
    ...
}

Further reading about input/output binding between parent and child components: https://angular.io/guide/inputs-outputs进一步阅读父子组件之间的输入/输出绑定: https://angular.io/guide/inputs-outputs

Update : If your alert component is no child of the master component, then a service can be used to share data between them.更新:如果您的警报组件不是主组件的子组件,则可以使用服务在它们之间共享数据。

Here is the implementation of a new service, that holds a boolean variable inside an BehaviourSubject:下面是一个新服务的实现,它在 BehaviourSubject 中包含一个 boolean 变量:

// share-service.ts
@Injectable()
export class ShareService {

    public isValid$: BehaviourSubject<boolean> = new BehaviourSubject<boolean>(false);

    constructor() {}

    public setValid(value: boolean): void {
        this.isValid$.next(value);
    }
}

I did not get, if your MasterComponent shall forward the data to the AlertComponent or vide versa, so in the following example, the MasterComponent updates the isValid of the ShareService, and the AlertComponent receives it.我没有得到,如果您的 MasterComponent 应将数据转发给 AlertComponent 或者反之亦然,所以在下面的示例中,MasterComponent 更新 ShareService 的 isValid,AlertComponent 接收它。

Now, the master component can inject the service and update the value for the behavioursubject.现在,主组件可以注入服务并更新行为主题的值。 For showcase, I update it inside the OnInit method, but you can use it anywhere, where suitable:为了展示,我在 OnInit 方法中更新了它,但您可以在任何合适的地方使用它:

// master.component.ts
export class MasterComponent implements OnInit {
    constructor(private shareService: ShareService) {}

    ngOnInit(): void {
        this.shareService.setValid(true); // update valid to true
    }
}

Now, the AlertComponent can also inject the same service and listen on the behaviourSubject for updates:现在,AlertComponent 也可以注入相同的服务并监听 behaviorSubject 以获取更新:

// alert.component.ts
export class AlertComponent implements OnInit {
    constructor(private shareService: ShareService) {}

    public ngOnInit(): void {
        this.shareService.isValid$.subscribe(isValid => {
            if (isValid) {
                // insert code for isValid === true
            } else {
                // insert code for isValid === false
            }
        });
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Angular2中使用另一个组件中的变量 - How to use a variable from a component in another in Angular2 如何在Angular7中的另一个组件中使用变量 - How to use a variable from a component in another in Angular7 是否可以使用角度为5的另一个组件中的某个组件的全局变量? - Is it possible to use a global variable from a component in another component in angular 5? 如何将一个组件中的变量用于 angular 中的另一个组件 - how to use the variable in one component to another component in angular 如何在Angular 2中使用一个类(组件)的变量在另一个类(组件)中? - How to use variable of one class(component) in another class(component) in Angular 2? Angular 2-如何使用另一个组件的功能? - Angular 2 - how to use function from another component? 如何以角方式将变量从一个组件访问另一个组件? - How to access variable from one component into another component in angular? 如何在Angular 5中将一个组件的变量更改为另一个组件的变量 - How to change variable of one component from another component in Angular 5 如何在 Angular 中将变量从组件检索到另一个? (不在 Html 页面中,而是在组件中) - How to retrieve variable from component to another in Angular? (not in the Html page, but in the component) 在 Angular 上的另一个组件(between.ts 文件)中使用一个变量 - Use a variable from one component in another (between .ts files) on Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM