【问题标题】:Angular - trying to use child component function in parent view but I'm gettting an errorAngular - 尝试在父视图中使用子组件功能,但出现错误
【发布时间】:2022-01-27 03:44:25
【问题描述】:

当我使用@ViewChild 时,我收到未定义组件的错误。 当我使用 @ViewChildren 时,我收到该组件中的函数不是函数的错误。

我不熟悉在 Angular 中使用子组件,所以当我确实在父组件中定义了子组件并且它显然是子组件中的一个函数时,我不确定它为什么会这样做。

我不想在父级中定义来自子级的每个函数,否则使用单独组件的意义何在。

子组件

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-mood',
  templateUrl: './mood.component.html',
  styleUrls: ['./mood.component.css']
})
export class MoodComponent implements OnInit {

  moodColors = ['red', 'orange', 'grey', 'yellow', 'green'];

  constructor() { }

  ngOnInit(): void {
  }

  chooseMood() {
    alert(this.moodColors);
  }

}

父组件(带有“ERROR TypeError: ctx_r3.mood is undefined”的版本相关部分)

import { Component, OnInit, ViewChild, ViewChildren } from '@angular/core';
import { ViewEncapsulation } from '@angular/core';
import { MoodComponent } from '../mood/mood.component';

@Component({
  selector: 'app-calendar',
  templateUrl: './calendar.component.html',
  styleUrls: ['./calendar.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class CalendarComponent implements OnInit {

  @ViewChild('mood') mood: MoodComponent = new MoodComponent;

父组件(带有“ERROR TypeError: ctx_r3.mood.chooseMood is not a function”的版本相关部分)

import { Component, OnInit, ViewChild, ViewChildren } from '@angular/core';
import { ViewEncapsulation } from '@angular/core';
import { MoodComponent } from '../mood/mood.component';

@Component({
  selector: 'app-calendar',
  templateUrl: './calendar.component.html',
  styleUrls: ['./calendar.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class CalendarComponent implements OnInit {

  @ViewChildren('mood') mood: MoodComponent = new MoodComponent;

父视图

<h2 (click)="mood.chooseMood()"></h2>

【问题讨论】:

  • 我忘了添加,我可以使用插值从父视图调用该子函数,所以我知道它可以看到它,但我不能将它用于我正在尝试做的事情。

标签: javascript html angular


【解决方案1】:

您没有通过new 显式初始化视图子级。

只需使用:

@ViewChild('mood') mood : MoodComponent;

如果这不起作用,请发布一个 Stackblitz 示例,我可以对其进行编辑以解决问题。

此外,在 Angular 中使用 ViewChild 更像是一个例外,您对它的使用表明可能存在设计问题。您的子组件更有可能通过 Output 向父组件发出。

关于输出,你可以做这样的事情——尽管如果不深入了解你想要达到的目标,很难给出准确的答案:

export class MoodComponent implements OnInit {
 @Input() moodId: string;
 @Output() chooseMood = new EventEmitter<string>();

  moodClicked(){
    this.chooseMood.emit(moodId);
  }
}

export class CalendarComponent implements OnInit {
  moodChosen(string: moodId){
    console.log(moodId);
  }
}

// Calendar template:

<app-mood
  moodId="happy"
  (chooseMood)="moodChosen($event)"
></app-mood>

【讨论】:

  • 我对输出不太熟悉。我可以使用该 1 输出中的任何方法或变量吗?
  • 您告诉我使用的代码 sn-p 在编译“成员 'mood' 隐式具有 'any' 类型之前给我一个错误。”
  • 这是因为它没有类型并且您的编译器设置“没有任何隐含” - 简单的解决方案是添加MoodComponentType,就像我在上面更新的那样。
  • 输入/输出允许我做我想做的事情,所以我会将此答案标记为正确。但只是为了使有关如何克服这些错误的答案合并到这个标记的答案中,我将复制它们并将它们粘贴到这个评论中:“Re your 'no initializer' error - see https://stackoverflow.com/a/50241920/1188074 - you have the default (overly) strict编译器设置也会导致您在 'any' 类型中看到的错误,尽管该设置对强制执行非常有用。” -wlf
【解决方案2】:

1 - 您必须使用此代码

@ViewChild('mood') mood : MoodComponent;

当您使用@ViewChildren 时,它将返回带有“心情”名称的项目列表,然后您必须使用此代码

mood.first.chooseMood() ;

当你的元素中有 ngIf 时,最好使用 ViewChildren

2- 初始化情绪变量不需要新关键字 它会在 ngOnInit 生命周期触发后被填充

【讨论】:

  • 如果我使用你的第一个例子。在我编译之前我得到了一个错误。 “属性 'mood' 没有初始化器,也没有在构造函数中明确分配。”
  • 如果我使用@ViewChild('mood') 心情!:MoodComponent;仍然说“ctx_r3.mood 未定义”
  • Re your 'no initializer' 错误 - 请参阅 stackoverflow.com/a/50241920/1188074 - 您有默认(过度)严格的编译器设置,这也导致了您在使用 'any' 类型时看到的错误,尽管那个执行起来相当有用。
  • 您可以搜索“非空断言运算符”,并像这样@ViewChild('mood') 心情更改代码! : 情绪成分;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
  • 1970-01-01
  • 2021-12-07
  • 2019-04-15
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
相关资源
最近更新 更多