属性 'nativeElement' 在 Angular2+ 中的类型 '() => void' 上不存在

Property 'nativeElement' does not exist on type '() => void' in Angular2+

我想在不点击按钮的情况下触发点击事件。所以我遵循了 post 中提到的结构 -

但是,我收到一个错误:

Property 'nativeElement' does not exist on type '() => void'

我在网上找不到相关的参考资料。

我想在点击 accurateExtract() 时触发 showAccurateGraphs() 按钮。

我的密码是-

app.component.html

<button type="button"   (click)="accurateExtract()" class="btn btn-
dark">Accurate Extract</button>

 <button type="button" #accurateExt (click)="showAccurateGraphs()" 
   class="btn btn-dark">Show Accurate API Graph</button>

app.component.ts

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

   import {  ViewChild, ElementRef, Renderer } from '@angular/core'


   constructor(private renderer:Renderer ) {}

   @ViewChild('accurateExt') accurateExt: ElementRef;


   accurateExtract(){


    let event = new MouseEvent('click', {bubbles: true});
    this.renderer.invokeElementMethod(
    this.accurateExtract.nativeElement,'dispatchEvent', [event]);

     }

如下更改您的代码,因为 invokeElementMethod()renderer API 2 in angular 中不再可用。你的 属性 有拼写错误,你应该把 this.accurateExtract 改成 this.accurateExt

 accurateExtract(){
    let event = new MouseEvent('click', {bubbles: true});
    this.accurateExt.nativeElement.dispatchEvent(event);
 }