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.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.
Global Properties and Methods in Vue 3.0:
Vue.createApp({ /* your app definition */ }).mount('#app');
Vue 3.0 createApp Global API:
createApp
is a global API to create a new Vue application instance.const app = Vue.createApp({ /* your app definition */ }); app.mount('#app');
Vue 3.0 provide and inject Global API:
provide
and inject
are used to share data between components without using props.// In the providing component app.provide('sharedData', someData); // In the consuming component const sharedData = app.inject('sharedData');
Vue 3.0 appConfig Global API:
appConfig
allows configuration of global app behavior.app.config.performance = true;
Vue 3.0 set and delete Global API:
set
and delete
methods allow dynamically modifying properties on the app's configuration.app.config.set('customOption', 'someValue'); app.config.delete('customOption');
Using Vue 3.0 Global API in Components:
const MyComponent = { created() { const globalData = this.$root.config.globalData; // Access global data in components } };
Vue 3.0 Global API vs Instance API:
// Global API app.config.performance = true; // Instance API const componentInstance = { data() { return { /* component-specific data */ }; } };
Vue 3.0 Configuration Options Global API:
app.config.optionMergeStrategies.customStrategy = (parent, child, vm, key) => { // Custom merge strategy for configuration options };
Vue 3.0 Global Properties and Lifecycle Hooks:
Vue.config.globalProperties.$customProperty = 'someValue'; app.component('my-component', { created() { console.log(this.$customProperty); } });
How to Use Global API for Plugin Development in Vue 3.0:
const MyPlugin = { install(app) { app.config.globalProperties.$myPluginMethod = () => { // Plugin method implementation }; } }; app.use(MyPlugin);
Vue 3.0 app Context and Global API:
const MyComponent = { setup(props, { app }) { const globalData = app.config.globalProperties.globalData; // Access global data using app context } };
Vue 3.0 Global API Changes from Vue 2:
createApp
method and the use of provide
and inject
for component communication.// Vue 2 new Vue({ /* options */ }); // Vue 3 const app = Vue.createApp({ /* options */ }); app.mount('#app');