从内联元素访问 Web 组件中的类函数 [英] Access class function in Web Component from inline Element

查看:25
本文介绍了从内联元素访问 Web 组件中的类函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的 Web 组件中的元素执行定义的类函数:

I want to execute a defined class function from an Element inside my Web Component:

customElements.define('first-component', class FirstComponent extends HTMLElement {
    constructor() {
        super();
     }

     log() {
        console.log('Well Done!')
     }

     connectedCallback() {
        this.innerHTML = '<button onclick="log()">Do it</button>'
     }
});

现在状态:ReferenceError:未定义日志

推荐答案

With parentElement, or Nearest()

为了调用自定义元素的 log() 方法,您必须获得它的引用.

In order to call the log() method of the custom element, you'll have to get a reference on it.

在您的示例中,自定义元素是

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆