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 global API

Vue.js is an open-source JavaScript framework for building user interfaces and single-page applications. In Vue 3, the new global API is introduced, and it has some changes compared to Vue 2.

The following is an example of how you might set up a new Vue 3 application using the new global API.

Step 1: Installation

First, you have to install Vue 3. You can do this using npm (Node Package Manager):

npm install vue@next

Step 2: Create a Vue Application

Now let's create a new Vue application. Instead of creating a new Vue instance using new Vue(), in Vue 3, you use createApp() function.

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

createApp(App).mount('#app')

Here, we first import the createApp function from the vue module. We also import the App component, which will be our root component. We then call createApp with App as an argument to create a new application instance and mount it to an element with the ID of app.

Step 3: Global Configurations

In Vue 3, global configurations are set on the application instance, not on the Vue global as in Vue 2. For example, to disable production tips:

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

const app = createApp(App)
app.config.productionTip = false

app.mount('#app')

Step 4: Use Plugins

To use plugins in Vue 3, you call use() on the application instance. Here's an example with Vue Router:

import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import App from './App.vue'
import Home from './components/Home.vue'
import About from './components/About.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', component: Home },
    { path: '/about', component: About },
  ],
})

const app = createApp(App)
app.use(router)
app.mount('#app')

Here, we first create a router using the createRouter function from vue-router, defining some routes. We then create our application instance and call use(router) on it before mounting it.

Step 5: Global Components and Directives

To define a global component or directive in Vue 3, you use component() or directive() on the application instance. For example:

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

const app = createApp(App)

app.component('my-component', MyComponent)

app.mount('#app')

In this example, we import a component MyComponent and register it globally using app.component().

The same applies to global directives:

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

const app = createApp(App)

app.directive('focus', {
  mounted(el) {
    el.focus()
  },
})

app.mount('#app')

Here, we create a global directive named focus which focuses the element when it is inserted into the DOM.

That's it! You've now seen a simple overview of how the new global API in Vue 3 works.

  1. Global Properties and Methods in Vue 3.0:

    • Description: Vue 3.0 introduces a set of global properties and methods available across the application.
    • Code Example:
      Vue.createApp({ /* your app definition */ }).mount('#app');
      
  2. Vue 3.0 createApp Global API:

    • Description: createApp is a global API to create a new Vue application instance.
    • Code Example:
      const app = Vue.createApp({ /* your app definition */ });
      app.mount('#app');
      
  3. Vue 3.0 provide and inject Global API:

    • Description: provide and inject are used to share data between components without using props.
    • Code Example:
      // In the providing component
      app.provide('sharedData', someData);
      
      // In the consuming component
      const sharedData = app.inject('sharedData');
      
  4. Vue 3.0 appConfig Global API:

    • Description: appConfig allows configuration of global app behavior.
    • Code Example:
      app.config.performance = true;
      
  5. Vue 3.0 set and delete Global API:

    • Description: set and delete methods allow dynamically modifying properties on the app's configuration.
    • Code Example:
      app.config.set('customOption', 'someValue');
      app.config.delete('customOption');
      
  6. Using Vue 3.0 Global API in Components:

    • Code Example:
      const MyComponent = {
        created() {
          const globalData = this.$root.config.globalData;
          // Access global data in components
        }
      };
      
  7. Vue 3.0 Global API vs Instance API:

    • Difference: Global API is used for configuration and application-level properties, while instance API is used within components.
    • Example:
      // Global API
      app.config.performance = true;
      
      // Instance API
      const componentInstance = {
        data() {
          return { /* component-specific data */ };
        }
      };
      
  8. Vue 3.0 Configuration Options Global API:

    • Code Example:
      app.config.optionMergeStrategies.customStrategy = (parent, child, vm, key) => {
        // Custom merge strategy for configuration options
      };
      
  9. Vue 3.0 Global Properties and Lifecycle Hooks:

    • Code Example:
      Vue.config.globalProperties.$customProperty = 'someValue';
      
      app.component('my-component', {
        created() {
          console.log(this.$customProperty);
        }
      });
      
  10. How to Use Global API for Plugin Development in Vue 3.0:

    • Code Example:
      const MyPlugin = {
        install(app) {
          app.config.globalProperties.$myPluginMethod = () => {
            // Plugin method implementation
          };
        }
      };
      
      app.use(MyPlugin);
      
  11. Vue 3.0 app Context and Global API:

    • Description: App context is accessible through components and provides access to global API.
    • Code Example:
      const MyComponent = {
        setup(props, { app }) {
          const globalData = app.config.globalProperties.globalData;
          // Access global data using app context
        }
      };
      
  12. Vue 3.0 Global API Changes from Vue 2:

    • Note: Vue 3.0 introduces changes in the global API, including the new createApp method and the use of provide and inject for component communication.
    • Example:
      // Vue 2
      new Vue({ /* options */ });
      
      // Vue 3
      const app = Vue.createApp({ /* options */ });
      app.mount('#app');