Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register local components with defineComponent in VueJS 3.0

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
  }
  // ...
}
like image 455
carcade Avatar asked Sep 29 '20 08:09

carcade


People also ask

How do I register a component in Vue 3?

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");

How do I register a component in Vuejs?

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.


1 Answers

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

like image 84
Boussadjra Brahim Avatar answered Dec 31 '22 19:12

Boussadjra Brahim