嵌套组件中的Angular 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文件。

如果我尝试添加

  @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:

使用ViewChild装饰工厂

// 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:

是完全正交的。

具体来说,这意味着当您导入header/header.com.ponent将其注册在NgModule中时,您使用带有不同外壳的模块指定器导入组件。例如。header/header.component

这是您可以并且应该解决的问题。只需确保所有进口都使用相同的情况即可。

我建议在各地使用小写的文件名和小写模块指定符。一个简单的搜索和替换应照顾好。

您是否考虑过尝试使用ViewChild?这应该允许您获取对标题组件的引用并调用ShowMessage((方法。

https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-view-child-child-child

相关内容

  • 没有找到相关文章

最新更新