Vue 'export default' vs 'new Vue' Vue 'export default' vs 'new Vue' vue.js vue.js

Vue 'export default' vs 'new Vue'


When you declare:

new Vue({    el: '#app',    data () {      return {}    })}

That is typically your root Vue instance that the rest of the application descends from. This hangs off the root element declared in an html document, for example:

<html>  ...  <body>    <div id="app"></div>  </body></html>

The other syntax is declaring a component which can be registered and reused later. For example, if you create a single file component like:

// my-component.jsexport default {    name: 'my-component',    data () {      return {}    }}

You can later import this and use it like:

// another-component.js<template>  <my-component></my-component></template><script>  import myComponent from 'my-component'  export default {    components: {      myComponent    }    data () {      return {}    }    ...  }</script>

Also, be sure to declare your data properties as functions, otherwise they are not going to be reactive.


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

Here is a great article that explain more about components https://frontendsociety.com/why-you-shouldnt-use-vue-component-ff019fbcac2e


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.

See https://vuejs.org/v2/guide/components-registration.html for more details.