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 badges
Add a ment  | 

1 Answer 1

Reset to default 10

In <script setup>, use the defineExpose() macro:

<script setup>
const customMethod = () => {⋯}
      

本文标签: javascriptExpose method creating a webcomponent using Vue3Stack Overflow