【问题标题】:Angular 2 call method in nested component嵌套组件中的Angular 2调用方法
【发布时间】: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


    【解决方案1】:

    使用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.component 以将其注册到您的NgModule 时,您使用具有不同大小写的模块说明符导入了该组件。例如。 header/header.component

    这是一个您可以而且应该解决的问题。只需确保所有导入使用相同的大小写即可。

    我建议在任何地方都使用小写文件名和小写模块说明符。一个简单的搜索和替换应该可以解决这个问题。

    【讨论】:

    • 我已经更新了我的问题。我曾尝试过该方法,但是当我添加该方法时,出现上述错误。
    【解决方案2】:

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

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

    【讨论】:

    • 我已经更新了我的问题。我确实尝试过,但这样做时出现错误。
    猜你喜欢
    • 2017-01-29
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 2016-08-04
    • 2017-12-31
    • 2018-05-05
    • 1970-01-01
    • 2019-05-19
    相关资源
    最近更新 更多