Vue.js 3.0 Tutorial

Vue.js 3.0 Component Advanced

Vue.js 3.0 Transitions & Animations

Vue.js 3.0 Reusable & Combinable

Vue.js 3.0 Advanced

Vue.js 3.0 Tools

Vue.js 3.0 Scale

Vue.js 3.0 Accessibility

Vue.js 3.0 Migrating from Vue2

Vue.js 3.0 Contribute Documentation

Vue.js 3.0 API References

Vue.js 3.0 Style Guide

Vue 3.0 instance property

In Vue 3, the way we create and handle Vue instances has changed significantly compared to Vue 2. The old new Vue() syntax is replaced with the createApp() function. This new API design is centered around the concept of an "application instance".

Additionally, in Vue 3, instance properties have been reorganized for better structure and consistency. For example, Vue 2 instance properties like $router, $store, $route, etc., are now accessed via the app.config.globalProperties object.

Here's an overview of how to use instance properties in Vue 3:

Step 1: Creating an Application Instance

First, you create an application instance by calling the createApp() function:

import { createApp } from 'vue';
import App from './App.vue';

const app = createApp(App);

Step 2: Adding Global Properties

You can add properties to app.config.globalProperties that will be accessible in all components:

app.config.globalProperties.$myProperty = 'This is a global property';

Now, $myProperty can be accessed from any component in your application.

Step 3: Accessing Global Properties in Components

Within a component, you can access these global properties using the this keyword:

export default {
  mounted() {
    console.log(this.$myProperty); // Outputs: 'This is a global property'
  },
};

Note: In the setup() function of the Composition API, this is not available. Therefore, if you want to access global properties inside setup(), you'll need to use the getCurrentInstance() method:

import { getCurrentInstance } from 'vue';

export default {
  setup() {
    const instance = getCurrentInstance();
    console.log(instance.appContext.config.globalProperties.$myProperty); 
    // Outputs: 'This is a global property'
  },
};

That's the basic usage of instance properties in Vue 3! They provide a way to share data and methods across your entire application. Be aware that these properties are global and should be used sparingly. For complex state management, consider using Vue's own Vuex or other state management libraries.

  1. Vue 3.0 Instance Data and Properties:

    • Description: Vue 3.0 instances contain data properties accessible within the component.
    • Code Example:
      <script>
      export default {
        data() {
          return {
            message: 'Hello Vue 3.0!',
          };
        },
      };
      </script>
      
  2. Accessing Instance Properties in Vue 3.0:

    • Code Example:
      <template>
        <div>{{ message }}</div>
      </template>
      
      <script>
      export default {
        data() {
          return {
            message: 'Hello Vue 3.0!',
          };
        },
      };
      </script>
      
  3. Vue 3.0 this.$data Property:

    • Description: this.$data allows direct access to the reactive data properties.
    • Code Example:
      <template>
        <div>{{ this.$data.message }}</div>
      </template>
      
      <script>
      export default {
        data() {
          return {
            message: 'Hello Vue 3.0!',
          };
        },
      };
      </script>
      
  4. Vue 3.0 Instance Methods and Computed Properties:

    • Code Example:
      <script>
      export default {
        data() {
          return {
            count: 0,
          };
        },
        methods: {
          increment() {
            this.count++;
          },
        },
        computed: {
          doubledCount() {
            return this.count * 2;
          },
        },
      };
      </script>
      
  5. Vue 3.0 Reactive Data Properties:

    • Description: Data properties are reactive in Vue 3.0, triggering automatic updates.
    • Code Example:
      <script>
      export default {
        data() {
          return {
            message: 'Hello Vue 3.0!',
          };
        },
        methods: {
          updateMessage() {
            this.message = 'Updated Message';
          },
        },
      };
      </script>
      
  6. Vue 3.0 Instance Lifecycle Hooks:

    • Code Example:
      <script>
      export default {
        beforeCreate() {
          console.log('Before Create Hook');
        },
        created() {
          console.log('Created Hook');
        },
        beforeMount() {
          console.log('Before Mount Hook');
        },
        mounted() {
          console.log('Mounted Hook');
        },
        beforeUpdate() {
          console.log('Before Update Hook');
        },
        updated() {
          console.log('Updated Hook');
        },
        beforeUnmount() {
          console.log('Before Unmount Hook');
        },
        unmounted() {
          console.log('Unmounted Hook');
        },
      };
      </script>
      
  7. Vue 3.0 Component Instance Properties:

    • Code Example:
      <script>
      export default {
        name: 'MyComponent',
      };
      </script>
      
  8. Vue 3.0 Instance Context:

    • Description: this within a component refers to the component's instance context.
    • Code Example:
      <script>
      export default {
        data() {
          console.log(this instanceof Vue); // true
        },
      };
      </script>
      
  9. Vue 3.0 Accessing Props in Component Instance:

    • Code Example:
      <script>
      export default {
        props: ['message'],
        mounted() {
          console.log(this.message);
        },
      };
      </script>
      
  10. Vue 3.0 Instance Events and Event Handlers:

    • Code Example:
      <template>
        <button @click="handleClick">Click me</button>
      </template>
      
      <script>
      export default {
        methods: {
          handleClick() {
            console.log('Button clicked');
          },
        },
      };
      </script>
      
  11. Vue 3.0 Global Properties vs Instance Properties:

    • Description: Global properties are shared across all instances, while instance properties are specific to each component.
    • Example:
      // Global Property
      Vue.config.globalProperties.$myGlobalProperty = 'Global Value';
      
      // Instance Property
      export default {
        created() {
          console.log(this.$myGlobalProperty);
        },
      };
      
  12. Vue 3.0 Instance Watch Property:

    • Code Example:
      <script>
      export default {
        data() {
          return {
            message: 'Hello Vue 3.0!',
          };
        },
        watch: {
          message(newVal, oldVal) {
            console.log(`Message changed from ${oldVal} to ${newVal}`);
          },
        },
      };
      </script>
      
  13. Vue 3.0 Dynamic Component Creation with Instance Properties:

    • Code Example:
      <template>
        <component :is="dynamicComponent"></component>
      </template>
      
      <script>
      export default {
        data() {
          return {
            dynamicComponent: 'MyComponent',
          };
        },
      };
      </script>