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 introduces the createApp
function, a new way to create a Vue application. This replaces the previous new Vue()
syntax used in Vue 2.x.
The new API provides a more straightforward way to create and configure a Vue application. Here's how you would create a basic Vue application with Vue 3.0:
import { createApp } from 'vue' import App from './App.vue' createApp(App).mount('#app')
In the above code, we import createApp
from vue
and use it to create a new Vue application. We then mount the application to an element with the id app
.
Let's build on this with a more comprehensive 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're using the component
method to globally register a component, MyComponent
. This component can now be used anywhere in our App
.
We can also use the directive
, mixin
, and use
methods to globally register directives, mixins, and plugins:
import { createApp } from 'vue' import App from './App.vue' import MyDirective from './MyDirective.vue' import MyMixin from './MyMixin' import MyPlugin from './MyPlugin' const app = createApp(App) app.component('my-component', MyComponent) app.directive('my-directive', MyDirective) app.mixin(MyMixin) app.use(MyPlugin) app.mount('#app')
In Vue 3.0, each call to createApp
will create a new application instance, isolated from others. As a result, global configurations (like component registration via app.component
, app.directive
, etc.) do not cross-contaminate between multiple apps.
Vue 3.0 Application Lifecycle Hooks:
<script> export default { beforeCreate() { console.log('Before Create Hook'); }, created() { console.log('Created Hook'); }, mounted() { console.log('Mounted Hook'); }, // ... other hooks }; </script>
Vue 3.0 configure
Method in Application API:
configure
method is used to modify global configurations of Vue.import { createApp } from 'vue'; const app = createApp(App); app.configure('production', () => { // configure for production }); app.mount('#app');
Vue 3.0 Handling Global Configuration in createApp
:
config
method in createApp
to set global configurations.import { createApp } from 'vue'; const app = createApp(App); app.config.productionTip = false; // Disable production tip app.mount('#app');
Vue 3.0 Creating a Vue Instance with createApp
:
createApp
to create a Vue instance and mount it to an element.import { createApp } from 'vue'; import App from './App.vue'; const app = createApp(App); app.mount('#app');
Vue 3.0 mount
and unmount
Methods in Application API:
mount
is used to mount the app to a specified element, and unmount
is used to unmount the app.import { createApp } from 'vue'; import App from './App.vue'; const app = createApp(App); const mountedApp = app.mount('#app'); // Later mountedApp.unmount();
Vue 3.0 globalProperties
in Application API:
globalProperties
allows you to add properties/methods globally accessible in components.import { createApp } from 'vue'; import App from './App.vue'; const app = createApp(App); app.config.globalProperties.$myGlobalMethod = () => { console.log('Global Method'); }; app.mount('#app');
Vue 3.0 Accessing App Context in Components:
app
context to access global properties or methods in components.<script> export default { mounted() { this.$appContext.config.globalProperties.$myGlobalMethod(); }, }; </script>