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
export default is used to create local registration for Vue component.
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.
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.
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.
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.
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.
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.
If you need to create multiple components I would highly recommend using Single File Components (SFC)
<script>
tag):import { defineComponent } from 'vue'
export default defineComponent({
// ...
})
(or export default {}
if not using TypeScript)
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')
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