【发布时间】:2017-05-03 23:05:54
【问题描述】:
我在这方面找到了一些较旧的答案,但他们使用的方法不再有效。我有一个标题组件
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:
【问题讨论】:
标签: angular typescript angular2-components