VueJS 3.0 provides new syntax for defining component
(https://v3.vuejs.org/api/global-api.html#arguments-3).
import { defineComponent, ref } from 'vue'
const HelloWorld = defineComponent(function HelloWorld() {
const count = ref(0)
return { count }
})
How to locally register components with passing function to defineComponent
?
In old style you are to use components
field in component definition object like this:
import ComponentA from './ComponentA.vue'
export default {
components: {
ComponentA
}
// ...
}
import Vue from "vue";import PopupWindow from "./components/PopupWindow";import App from "./App. vue";Vue. component("PopupWindow", PopupWindow); // global registration - can be used anywherenew Vue({ render: (h) => h(App),}). $mount("#app");
When we want to globally register a component in Vue, we need to do it in this file. All you have to do is import your component, as you usually would, and then register it using app. component . import { createApp } from 'vue' import App from './App.
You could do it simply as before by adding that component to components
option :
import ComponentA from './ComponentA.vue'
export default defineComponent({
setup() {
const count = ref(0)
return { count }
},
components: {
ComponentA
}
});
LIVE DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With