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

时间:2019-02-27 05:48:51

标签: javascript angular click angular2-forms

即使不单击按钮,我也想触发单击事件。所以我遵循了帖子中提到的结构- angular2 manually firing click event on particular element

但是,我遇到一个错误:

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

在网络上找不到相关的参考文献。

我想在点击precisionExtract()时触发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]);

     }

1 个答案:

答案 0 :(得分:0)

按以下步骤更改代码,因为renderer API 2中的invokeElementMethod()不再可用。您的媒体资源中存在拼写错误,应将this.accurateExtract更改为this.accurateExt

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