이전 답변을 찾았지만 사용하는 메서드는 더 이상 유효하지 않습니다. 나는 헤더 구성 요소 그것은 꽤 기본입니다중첩 된 구성 요소의 각도 2 호출 메서드
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 파일을 사용하지 않습니다.
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:
내 질문이 업데이트되었습니다. 그 방법을 시도했지만, 내가 그것을 추가 할 때 위의 오류가 발생합니다. – Jhorra
@Jhorra 문제가 해결되면이 대답에 –
투표를 업데이트했습니다. p –