Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mikromike's avatar

how to defineCustomElement and props ?

Hello I am stuck how to get props from customElement back to main component.

My customVue Component inside index.html.

my main.js

 import { defineCustomElement } from 'vue'
 import CustomBanner from './CustomBanner.ce.vue'
 import './index.css'

 props; {
    position: Number
 }

  const CustomElement = defineCustomElement(CustomBanner)
 customElements.define('custom-banner', CustomElement)

Now how to pass props to my main component: CustomBanner.ce.vue ?

I need to read to variable let slot = ref(0);

     <script setup>
      import axios from "axios";
     import { ref, onMounted, onUpdated, getCurrentInstance, onUnmounted } from "vue";
    import AdSlide from "./components/Ad.vue";
 --->   let slot = ref(0);   ????

I am out of ideas ! ,-C ... thanks Mika.

0 likes
1 reply
LaryAI's avatar
Level 58

To pass props to your custom element and use them in your CustomBanner.ce.vue component, you need to ensure that the props are properly defined and passed down. Here’s a step-by-step solution:

  1. Define Props in CustomBanner.ce.vue: Ensure that your CustomBanner.ce.vue component is set up to accept props.

  2. Pass Props to the Custom Element: When you use the custom element in your HTML, pass the props as attributes.

  3. Access Props in the Component: Use the props in your component as needed.

Here’s how you can do it:

Step 1: Define Props in CustomBanner.ce.vue

In your CustomBanner.ce.vue, define the props that you expect to receive. For example:

<template>
  <div>
    <!-- Use the prop in your template -->
    <p>Position: {{ position }}</p>
  </div>
</template>

<script setup>
import { ref } from 'vue';

// Define props
const props = defineProps({
  position: Number
});

// Use the prop in your script
let slot = ref(props.position);
</script>

Step 2: Pass Props to the Custom Element

When you use the custom element in your HTML, pass the props as attributes. For example, in your index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Custom Element Example</title>
</head>
<body>
  <!-- Pass the position prop as an attribute -->
  <custom-banner position="5"></custom-banner>

  <script src="path/to/your/main.js"></script>
</body>
</html>

Step 3: Ensure Your Main.js is Correct

Your main.js should already be set up correctly to define and register the custom element. Here’s a quick check:

import { defineCustomElement } from 'vue';
import CustomBanner from './CustomBanner.ce.vue';
import './index.css';

// Define the custom element
const CustomElement = defineCustomElement(CustomBanner);

// Register the custom element
customElements.define('custom-banner', CustomElement);

Summary

By defining the props in your CustomBanner.ce.vue component and passing them as attributes in your HTML, you can successfully pass data from your custom element to your Vue component. The defineProps function in the <script setup> block allows you to access these props within your component.

If you follow these steps, you should be able to pass and use props in your custom Vue component.

Please or to participate in this conversation.