admin管理员组文章数量:1304080
I am creating a web-ponent using VueJS 3, I want to expose a method on the ponent allowing the user to do something like this:
<custom-ponent id="x1" />
<script>
var ponent = document.getElementById("x1");
ponent.customMethod(); // as focus() method on native elements
</script>
If I define a method on the ponent, I can call the method inside the ponent. But the method is not available when I use it as a web-ponent.
//main.ts/js
import { defineCustomElement } from "vue"
import ponent from "./ponent.ce.vue"
const element = defineCustomElement(ponent );
customElements.define("custom-ponent ", element);
//ponent.ce.vue
const customMethod = () => { console.log("Executed"); }
How I can indicate to Vue Component Wrapper that the customMethod will be available outside the ponent?
I am creating a web-ponent using VueJS 3, I want to expose a method on the ponent allowing the user to do something like this:
<custom-ponent id="x1" />
<script>
var ponent = document.getElementById("x1");
ponent.customMethod(); // as focus() method on native elements
</script>
If I define a method on the ponent, I can call the method inside the ponent. But the method is not available when I use it as a web-ponent.
//main.ts/js
import { defineCustomElement } from "vue"
import ponent from "./ponent.ce.vue"
const element = defineCustomElement(ponent );
customElements.define("custom-ponent ", element);
//ponent.ce.vue
const customMethod = () => { console.log("Executed"); }
How I can indicate to Vue Component Wrapper that the customMethod will be available outside the ponent?
Share Improve this question edited Feb 21, 2022 at 20:57 tony19 139k23 gold badges277 silver badges347 bronze badges asked Feb 20, 2022 at 19:56 David SKDavid SK 8247 silver badges21 bronze badges1 Answer
Reset to default 10In <script setup>
, use the defineExpose()
macro:
<script setup>
const customMethod = () => {⋯}
本文标签:
javascriptExpose method creating a webcomponent using Vue3Stack Overflow
版权声明:本文标题:javascript - Expose method creating a web-component using Vue3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1741779533a2397227.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论