简体   繁体   English

从共享服务中调用组件的功能

[英]Call function of a component from within a shared service

I am trying to set up sibling communication by using a shared service. 我试图通过使用共享服务来建立同级通信。 I'll start by showing you the relevant components: 首先,向您展示相关组件:

parent component: 父组件:

import { SerializationService } from './serialization/serialization.service';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css'],
    providers: [SerializationService]
})
export class AppComponent implements OnInit {
    //irrelevant body
}

The shared service: 共享服务:

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

@Injectable()
export class SerializationService {

    serialize() {
         //NEED TO CALL getData() IN DataComponent!!!
    }
}

The two sibling components: 两个同级组件:

//Toolbar component: 
import { SerializationService } from '../serialization/serialization.service';

@Component({
  selector: 'toolbar-component',
  templateUrl: './toolbar.component.html',
  styleUrls: ['./toolbar.component.css']
})
export class ToolbarComponent {

  constructor(private serializationService : SerializationService) { }

  serialize() {
      this.serializationService.serialize();
  }
}

//Data Component: 
import { SerializationService } from "../serialization/serialization.service";

@Component({
  selector: 'pixi-component',
  templateUrl: './pixi.component.html',
  styleUrls: ['./pixi.component.css'],
})
export class DataComponent {

    private data : String;

    constructor(private operationService: OperationService, 
                private serializationService : SerializationService
    ){}

    getData() : String {
        return this.data;
    }
}

You can see that ToolbarComponent calls the serialize() function of SerializationService . 您可以看到ToolbarComponent调用了SerializationServiceserialize()函数。 The service needs some data to serialize, so it needs to call the getData() function from DataComponent and catch it's return. 该服务需要一些数据进行序列化,因此需要从DataComponent调用getData()函数并捕获其返回值。 Both components are children of AppComponent (they are siblings). 这两个组件都是AppComponentAppComponent (它们是同级)。

How can I do this? 我怎样才能做到这一点?

It's not a good idea to call a component's method from a service, maybe it is not even possible. 从服务中调用组件的方法不是一个好主意,也许甚至不可能。 Your component use services, not the opposite. 您的组件使用服务,而不是相反。

But you can get data from DataComponent into ToolbarComponent, for example by referencing sibling using template reference variable and using, for example, an input in TollbarComponent to receive the data: 但是您可以将数据从DataComponent获取到ToolbarComponent中,例如通过使用模板引用变量引用同级并使用例如TollbarComponent中的输入来接收数据:

app.component.html: app.component.html:

<toolbar-component [dataToSerialize] = "#dataComponent.getData()"></toolbar-component>
<pixi-component #dataComponent></pixi-component>

You then pass the data to SerializationService in the serialize method of ToolbarComponent: 然后,将数据通过ToolbarComponent的serialize方法传递给SerializationService:

export class ToolbarComponent {

    constructor(private serializationService : SerializationService) { }
     @Input() dataToSerialize;

    serialize() {
        this.serializationService.serialize(this.dataToSerialize);
    }
}

Depending on your application's logic, you can also pass the data to ToolbarComponent on some event binding, for example on click event: 根据应用程序的逻辑,还可以通过某些事件绑定(例如,在click事件上)将数据传递到ToolbarComponent:

app.component.html: app.component.html:

<toolbar-component (click)="serialize(dataComponent.getData())"></toolbar-component>
<pixi-component #dataComponent></pixi-component>

// Toolbar component: //工具栏组件:

export class ToolbarComponent {

    constructor(private serializationService : SerializationService) { }

    serialize(data) {
        this.serializationService.serialize(data);
    }
}

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

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