不能在div onclick中使用方法

问题描述 投票:0回答:1

我有一个角度样本,下面是我的app.component.ts文件:

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

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class DashboardComponent implements OnInit {
  title = 'Core Helper';
  
  constructor(private router: Router) { 
    // this.router = Router;
  }

  navigate() {
      this.router.navigate(["docs"]);
  }
  }

app.component.html

<div id="releasenotes" onclick="navigate()">Release Notes</div>

当我调用此方法时,它引发以下错误:

dashboard:27 Uncaught ReferenceError: navigate is not defined
    at HTMLDivElement.onclick (dashboard:27)

我不知道如何解决这个问题,我已经尝试过如下

 document.getElementById('releasenotes').addEventListener("click", this.navigate);

在这种情况下,路由器未定义

javascript angular routing navigation
1个回答
0
投票

Click事件的编写应如下所示,

<div id="releasenotes" (click)="navigate()">Release Notes</div>
© www.soinside.com 2019 - 2024. All rights reserved.