Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which way better to create vue component (export default vs defineComponent vs new Vue)

After learning Vue.js lately, i'm pretty match confused about how to write vue component syntax i keep seeing youtube tutorials, as well as articles, and everyone uses a different approach.

in terms of vue 3 should we use export default to create a component or export default defineComponent or new Vue({

so how to decide the right way on how to create App component and the rest of its child components and pages etc ..

Hopefully my question is clear enough.

Thanks

like image 988
SystemX Avatar asked Apr 05 '21 17:04

SystemX


People also ask

Why do we use export default in Vue?

export default is used to create local registration for Vue component.

Should I use Vue 2 or vue3?

If you are on a very large and complex existing project using Vue 2: You may not want to migrate to Vue 3, depending on your code, the migration time and performance benefits may not be worth it. If you are facing performance issues after doing various optimizations, then use Vue 3.

Can I use Vue 3 or should I still use Vue 2 for a new project?

Most of the syntax and practices in Vue 2 remain the same in Vue 3, so it shouldn't make a big difference if you learn Vue 2 or Vue 3. Most libraries now support Vue 3 and we are also expecting a compatibility version of Vue 3 to be launched soon.

What is the difference between export default and new Vue()?

The first case (export default {...})is ES2015 syntax for making some object definition available for use. The second case (new Vue (...)) is standard syntax for instantiating an object that has been defined. The first will be used in JS to bootstrap Vue, while either can be used to build up components and templates.

How do I export components from a Vue template?

You can also use an ID selector pointing to an element (usually native <template> elements) - Vue will use its content as the template source. The example above defines a single component and exports it as the default export of a .js file, but you can use named exports to export multiple components from the same file.

How to create a Vue component with Vue CLI?

Each component is in a .vue file. All components must include a template tag and a script. A style tag may also be added to style the template. Here’s an example of a simple Vue component created with Vue CLI: The object after export default is just like the component object in the CDN version.

Why should I use Vue instead of HTML?

It's common for an app to be organized into a tree of nested components: This is very similar to how we nest native HTML elements, but Vue implements its own component model that allow us to encapsulate custom content and logic in each component. Vue also plays nicely with native Web Components.


Video Answer


1 Answers

If you need to create multiple components I would highly recommend using Single File Components (SFC)

Here you define a new component as (inside the <script> tag):

import { defineComponent } from 'vue'
export default defineComponent({
  // ...
})

(or export default {} if not using TypeScript)

For the main app component you would do this:

import { createApp } from "vue";
const app = createApp(App)
app.mount('#app')

OR just like this, if you don't need to extent Vue with vue-router, Vuex etc.

import { createApp } from "vue";
createApp(App).mount('#app')
like image 92
tauzN Avatar answered Oct 20 '22 08:10

tauzN