简体   繁体   English

Angular在两个组件之间共享服务实例

[英]Angular sharing the instance of a service between two components

I am working on a code where I need to share the instance of the service. 我正在编写需要共享服务实例的代码。 Here is the sample outline of the code where I am facing a trouble. 这是我遇到麻烦的代码示例大纲。

app.component.ts code: app.component.ts代码:

import { Component } from '@angular/core';
import {ROUTER_DIRECTIVES} from '@angular/router';
import {HeaderComponent} from './header.component';
import {TotalContainer} from './container-total.component';
@Component({
  selector: 'my-app',
  template: `
    <myheader [display]="display"></myheader>
    <router-outlet></router-outlet>
  `,
  directives:[HeaderComponent,TotalContainer,ROUTER_DIRECTIVES]
})
export class AppComponent { 
    display = true;
}

My header component contains the nav-tabs which will update the 我的标头组件包含导航标签,这些标签会更新

Now, one of the component which will replace the is container-total.component.ts 现在,将替换的组件之一是container-total.component.ts

Now Inside the container-total.component.ts I have another component called bottom.component. 现在,在container-total.component.ts内部,我有另一个名为bottom.component的组件。

Here the code for container-total.component.ts 这是container-total.component.ts的代码

import {Component, OnInit} from '@angular/core';
import {BottomContainerComponent} from './bottom-container.component';
import {BlurredService} from '../services/blurred.service';
import {FollowUps} from '../properties/followups';

@Component({
    selector:'container-total',
    template:`

    <div class="container" [class.blurred]="!display">
         My Content of total container has to be blurred.
    </div>    
    <bottom-container></bottom-container>
    `,
    styles:[`
        .blurred{
    -webkit-filter: blur(1px);
    -moz-filter: blur(1px);
    -o-filter: blur(1px);
    -ms-filter: blur(1px);
    filter: blur(1px) grayscale(90%);
    }
    `],
    directives:[BottomContainerComponent],
    providers:[BlurredService]
})
export class TotalContainer implements OnInit{

    display;
    constructor(private blurredService: BlurredService){

    }

    checkBlurr(){
        this.display = this.blurredService.getService();
    }

    ngOnInit(){
        this.checkBlurr();
    }
}

Now, I am updating the value for the display in the bottom-container.component using the same service. 现在,我使用相同的服务来更新bottom-container.component中显示的值。 Here is the code 这是代码

bottom-container.component.ts bottom-container.component.ts

import {Component, Output, EventEmitter, OnInit} from '@angular/core';
import {BlurredService} from '../services/blurred.service';

@Component({
    selector:'bottom-container',
    template:`
    <div class="container" [class.blurred]="!display"    (click)="displayPopup()">
         This is the bottom container needed to be blurred.
    </div>
    `,
    styles:[`

        .blurred{
            -webkit-filter: blur(1px);
            -moz-filter: blur(1px);
            -o-filter: blur(1px);
            -ms-filter: blur(1px);
            filter: blur(1px) grayscale(90%);
        }
    `],
    directives:[PopupComponent]    
})
export class BottomContainerComponent implements OnInit{
    display;  
    request:Requests = null;

    constructor(private blurredService: BlurredService){

    }

    checkBlurr(){
        this.display = this.blurredService.getService();
    }

    ngOnInit(){
        this.checkBlurr();
    }

    displayPopup(){
        this.blurredService.setService(false);
        this.display = this.blurredService.getService();
    }
}

Now, this is the service I have written. 现在,这是我编写的服务。

blurred.service 模糊服务

import {Injectable} from '@angular/core';

@Injectable()
export class BlurredService{

    display = true;
    setService(value){
        this.display = value;
    }

    getService(){
        return this.display;
    }
}

Now, When ever the div in bottom-container is getting clicked it is getting blurred. 现在,每当单击底部容器中的div时,它就会变得模糊。 But not the div in the total-container, though I am updating the value through the service. 尽管我正在通过服务更新值,但是总容器中的div不是。

Should I call the function "checkBlurr" in total-container using EventEmitter from bottom-container. 我应该使用底部容器的EventEmitter在总容器中调用函数“ checkBlurr”吗? But if I do that, I might have more than one components in total-container for which I am going to implement the routing. 但是,如果这样做,我可能要在总容器中有多个组件来实现路由。 Then how should I use the @Output and EventEmitter using the "". 然后,如何使用“”来使用@Output和EventEmitter。

Can any one help me with this? 谁能帮我这个?

You need to do something with your component interaction. 您需要对组件交互做一些事情。 The easiest thing would be to set the "display" property in your total component directly to the service: 最简单的方法是将总组件中的“ display”属性直接设置为服务:

<div class="container" [class.blurred]="!blurredService.getService()">
    My Content of total container has to be blurred.
</div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM