事件未发出

时间:2017-06-04 13:24:02

标签: angular events service

目前我正在使用Angular4。现在我有一个问题。我有public void openAudioPlayer(View view){ LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); // Custom dialog layout View dialogView = layoutInflater.inflate(R.layout.dialog_audio_player, null); // PLAY/PAUSE button ToggleButton playStateButton = (ToggleButton) dialogView.findViewById(R.id.playStateButton); playStateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // DO something... } }); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.audio_player_title) .setMessage(R.string.audio_player_description) .setView(dialogView) .setPositiveButton(R.string.confirm, null) .setNegativeButton(R.string.cancel, null); // Create dialog AlertDialog alertDialog = builder.create(); // Show dialog alertDialog.show(); } 来更改HeaderService中的标题:

HeaderComponent

这是我的import {EventEmitter, Injectable, Output} from "@angular/core"; @Injectable() export class HeaderService { @Output() public fire: EventEmitter<String> = new EventEmitter(); constructor() { } public changeTitle(title: string) { console.log("Want to change title to: " + title); this.fire.emit(title); } public getTitle(): EventEmitter<String> { return this.fire; } }

HeaderComponent

在我的import {Component, EventEmitter, OnInit} from "@angular/core"; import {HeaderService} from "../../services/header.service"; @Component({ selector: 'header', templateUrl: './header.component.html' }) export class HeaderComponent implements OnInit { private title: string = "felixoi.net"; private subscription: EventEmitter<String>; constructor(private headerService: HeaderService) { } ngOnInit(): void { console.log("Loading HeaderComponent"); this.subscription = this.headerService.getTitle().subscribe((title: string) => { console.log("Changed title to: " + title); this.title = title; }); } } 我使用此服务更改标题:

HomeComponent

我在import {Component, OnInit} from '@angular/core'; import {HeaderService} from "../shared/services/header.service"; @Component({ selector: 'home', templateUrl: './home.component.html' }) export class HomeComponent implements OnInit { constructor(private headerService: HeaderService) { } ngOnInit(): void { console.log("Loading HomeComponent"); this.headerService.changeTitle("Home"); } } 中有完全相同的调用,但在此组件中,相同的代码无效。

TokenService

我在代码中创建了一些调试消息。这是控制台输出:

import { Component } from '@angular/core';
import {HeaderService} from "../shared/services/header.service";

@Component({
    selector: 'token',
    templateUrl: './token.component.html'
})
export class TokenComponent implements OnInit {

    constructor(private headerService: HeaderService) {
    }

    ngOnInit(): void {
        console.log("Loading TokenComponent");

        this.headerService.changeTitle("Tokens");
    }

}

它显示事件未在Loading HeaderComponent Loading HomeComponent Want to change title to: Home Changed title to: Home Loading TokenComponent Want to change title to: Tokens 上发出,但我不知道原因。

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。

模块是多次导入的,因此存在不同的实例,也有不同的发射器可以订阅。