Al intentar ViewChild, aparece el error. El error es "No se proporcionó un argumento para 'opts'".
Tanto @ViewChild está dando el error.
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): error TS2554: se esperaban 2 argumentos, pero se obtuvo 1.
En Angular 8, ViewChild tiene otro parámetro
@ViewChild('nameInput', {static: false}) component : ComponentPuedes leer más al respecto aquí y aquí
En Angular 9 , el valor predeterminado es static: false , por lo que no es necesario proporcionar un parámetro a menos que desee usar {static: true}
es porque la vista infantil requiere dos argumentos, intente así
@ViewChild('nameInput', { static: false, }) nameInputRef: ElementRef;
@ViewChild('cantidadEntrada', { static: false, }) cantidadEntradaRef: ElementoRef;
En Angular 8, ViewChild toma 2 parámetros
@ViewChild(ChildDirective, {static: false}) ComponentEso también resolvió mi problema.
@ViewChild('map', {static: false}) googleMap;En Angular 8, ViewChild siempre toma 2 parámetros, y los segundos parámetros siempre tienen estático: verdadero o estático: falso
Puedes probar así:
@ViewChild('nameInput', {static: false}) component Además, el static: false será el comportamiento alternativo predeterminado en Angular 9.
¿Qué son falsos/verdaderos estáticos? Por lo tanto, como regla general, puede optar por lo siguiente:
{ static: true } debe establecerse cuando desee acceder a ViewChild en ngOnInit.
Solo se puede acceder a { static: false } en ngAfterViewInit. Esto también es lo que desea buscar cuando tiene una directiva estructural (es decir, * ngIf) en su elemento en su plantilla.
En Angular 8, ViewChild toma 2 parámetros:
Prueba así:
@ViewChild('nameInput', { static: false }) nameInputRef: ElementRef;Explicación:
{ estático: falso }
Si establece static false , el componente secundario SIEMPRE se inicializa después de la inicialización de la vista a tiempo para las funciones de devolución de llamada ngAfterViewInit/ngAfterContentInit .
{ estático: verdadero}
Si establece static true , la inicialización del componente secundario tendrá lugar en la inicialización de la vista en ngOnInit
De forma predeterminada, puede usar { static: false } . Si está creando una vista dinámica y quiere usar la variable de referencia de la plantilla, debe usar { static: true}
Para más información, puedes leer este artículo
En la demostración, nos desplazaremos a un div usando la variable de referencia de plantilla.
@ViewChild("scrollDiv", { static: true }) scrollTo: ElementRef; Con { static: true } , podemos usar this.scrollTo.nativeElement en ngOnInit , pero con { static: false } , this.scrollTo no estará undefined en ngOnInit , por lo que podemos acceder solo en ngAfterViewInit
deberías usar el segundo argumento con ViewChild así:
@ViewChild("eleDiv", { static: false }) someElement: ElementRef;Regex para reemplazar todo a través de IDEA (probado con Webstorm)
Buscar: \@ViewChild\('(.*)'\)
Reemplazar: \@ViewChild\('$1', \{static: true\}\)
Utilizar esta
@ViewChild(ChildDirective, {estático: falso}) Componente
En Angular 8, ViewChild tiene otro parámetro
@ViewChild('nameInput', {estático: falso}) componente
Resolví mi problema como a continuación
@ViewChild(MatSort, {estático: falso}) ordenar: MatSort;
Prueba esto en angular 8.0:
@ViewChild('result',{static: false}) resultElement: ElementRef;