Угловая ошибка @ViewChild(): ожидается 2 аргумента, но получено 1 - программирование
Подтвердить что ты не робот

Угловая ошибка @ViewChild(): ожидается 2 аргумента, но получено 1

При попытке просмотра Viewchild я получаю ошибку. ошибка: "Не указан аргумент для" opts "."

Оба @ViewChild выдает ошибку.

import { Component, OnInit, ElementRef, ViewChild, Output, EventEmitter } from '@angular/core';
import { Ingredient } from 'src/app/shared/ingredient.model';

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

@ViewChild('nameInput') nameInputRef: ElementRef;
@ViewChild('amountInput') amountInputRef: ElementRef;
@Output() ingredientAdded = new EventEmitter<Ingredient>();
  constructor() {}

  ngOnInit() {
  }

  onAddItem() {
    const ingName = this.nameInputRef.nativeElement.value;
    const ingAmount = this.amountInputRef.nativeElement.value;
    const newIngredient = new Ingredient(ingName, ingAmount);
    this.ingredientAdded.emit(newIngredient);
  }

}

ts (11,2): ошибка TS2554: ожидалось 2 аргумента, но получен 1.

4b9b3361

Ответ 1

В Angular 8 ViewChild принимает 2 параметра

 @ViewChild(ChildDirective, {static: false}) Component

Ответ 2

В Angular 8 у ViewChild есть другой параметр

@ViewChild('nameInput', {static: false}) component

Вы можете узнать больше об этом здесь и здесь

Ответ 3

это потому, что вид ребенка требует два аргумента, попробуйте так

@ViewChild ('nameInput', {static: false,}) nameInputRef: ElementRef;

@ViewChild ('amountInput', {static: false,}) amountInputRef: ElementRef;

Ответ 4

@ViewChild ('map', {static: false}) googleMap; это также решило мою проблему