我已经找到了一些较旧的答案,但他们使用的方法已不再有效。我有一个标题组件
import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import { AuthenticationService } from '../_services/Authentication.Service';
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
loading = false;
error = '';
isLoggedIn = false;
showMessageWindow = false;
messageType: string = "";
messageText: string = "";
public currentUser: any = null;
constructor(private _auth: AuthenticationService, private router: Router) { }
ngOnInit() {
if(this._auth.isLoggedIn()) {
this.isLoggedIn = true;
}
else {
this._auth.logout();
this.isLoggedIn = false;
this.router.navigate(['/']);
}
}
showMessage(message: string, type: string) {
this.messageText = message;
this.messageType = type;
this.showMessageWindow = true;
}
}
这是非常基本的,显示和管理可以看到的导航,具体取决于是否以及谁登录。我在标题组件中内置了警告/警报。并非所有页面都使用标题,因此我将其导入到使用它的组件中,并将<app-header></app-header>
置于顶部,将其放入组件模板中。
Here is a component that uses the header.
import { Component, OnInit } from '@angular/core';
import { HeaderComponent } from '../header/Header.Component';
import { Router, ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-census',
templateUrl: './census.component.html',
styleUrls: ['./census.component.css']
})
export class CensusComponent implements OnInit {
constructor( private router: Router, private activatedRoute: ActivatedRoute) { }
ngOnInit() {
}
}
我希望能够将一个方法放入此组件中,该方法从标头组件调用showMessage()
。我尝试了几种不同的方法,但收效甚微。我最大的问题是如何引用嵌入式组件。我查看了文档,但是他们总是在组件中使用模板而不使用单独的html文件。
如果我尝试添加
@ViewChild(HeaderComponent)
private header: HeaderComponent;
进入我的CensusComponent我得到一个警告:
WARNING in ./src/app/header/Header.Component.ts
There are multiple modules with names that only differ in casing.
This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
Use equal casing. Compare these module identifiers:
答案 0 :(得分:2)
使用uptime
装饰工厂
perl
您收到的错误:
ViewChild
完全正交。
具体来说,这意味着当您导入header / Header.component以在// Here is a component that uses the header.
import {ViewChild} from '@angular/core';
import {Component, OnInit} from '@angular/core';
import {HeaderComponent} from '../header/Header.Component';
import {Router, ActivatedRoute} from '@angular/router';
@Component({
selector: 'app-census',
templateUrl: './census.component.html',
styleUrls: ['./census.component.css']
})
export class CensusComponent implements OnInit {
// The argument `HeaderComponent` tells the framework to bind to the child
// component whose constructor function is `HeaderComponent`
@ViewChild(HeaderComponent) header: HeaderComponent;
constructor(readonly router: Router, readonly activatedRoute: ActivatedRoute) {}
ngOnInit() {
this.header.showMessage('an error occurred!', 'error');
}
}
中注册它时,您使用具有不同外壳的模块说明符导入了该组件。例如。 WARNING in ./src/app/header/Header.Component.ts
There are multiple modules with names that only differ in casing.
This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
Use equal casing. Compare these module identifiers:
这是一个你可以而且应该修复的问题。只需确保所有导入都使用相同的大小写。
我建议在任何地方使用小写文件名和小写模块说明符。一个简单的搜索和替换应该照顾它。
答案 1 :(得分:1)
您是否考虑过尝试使用ViewChild?这应该允许您获取对标头组件的引用并调用showMessage()方法。
https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-view-child