Last active
November 4, 2022 10:12
-
-
Save pixelmattersdev/92255daae4161d1022d30c9f3a1b2d6c to your computer and use it in GitHub Desktop.
How to migrate from Vue 2 to Vue 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// package.json | |
{ | |
// ... | |
"dependencies": { | |
"vue": "^3.2.0", // UPDATE | |
"@vue/compat": "^3.2.0" // ADD | |
// ... | |
}, | |
"devDependencies": { | |
"@vue/compiler-sfc": "^3.2.0" // ADD | |
"vue-template-compiler": "^2.6.0" // REMOVE | |
// ... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// webpack.config.js | |
module.exports = { | |
// ... | |
resolve: { | |
alias: { | |
vue: '@vue/compat' | |
} | |
}, | |
module: { | |
rules: [ | |
{ | |
test: /\.vue$/, | |
loader: 'vue-loader', | |
options: { | |
compilerOptions: { | |
compatConfig: { | |
// Default everything to Vue 2 behavior | |
MODE: 2 | |
} | |
} | |
} | |
} | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<CustomDialog> | |
<template slot="heading" slot-scope="slotProps"> | |
<h1>Items ({{ slotProps.items.length }})</h1> | |
</template> | |
</CustomDialog> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<CustomDialog> | |
<template #heading="slotProps"> | |
<h1>Items ({{ slotProps.items.length }})</h1> | |
</template> | |
</CustomDialog> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template functional> | |
<h1>{{ title }}</h1> | |
</template> | |
<script> | |
export default { | |
props: { | |
title: { | |
type: String, | |
required: true, | |
}, | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<h1>{{ title }}</h1> | |
</template> | |
<script> | |
export default { | |
props: { | |
title: { | |
type: String, | |
required: true, | |
}, | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div | |
v-if="list" | |
v-for="item in list" | |
:key="item.id" | |
:title="item.title"> | |
{{ item.title }} | |
</div> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<template v-if="list"> | |
<div | |
v-for="item in list" | |
:key="item.id" | |
:title="item.title"> | |
{{ item.title }} | |
</div> | |
</template> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<ul> | |
<li v-for="item in list"> | |
<p v-if="item.amount < 10" :key="item.id">{{ item.title }}</p> | |
<p v-else class="high" :key="item.id">{{ item.title }}</p> | |
</li> | |
</ul> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<ul> | |
<li v-for="item in list"> | |
<p v-if="item.amount < 10">{{ item.title }}</p> | |
<p v-else class="high">{{ item.title }}</p> | |
</li> | |
</ul> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<template v-for="item in list"> | |
<div :key="item.id">{{ item.title }}</div> | |
</template> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<template v-for="item in list" :key="item.id"> | |
<div>{{ item.title }}</div> | |
</template> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style lang="scss"> | |
.v-enter, | |
.v-leave-to { | |
opacity: 0; | |
} | |
.v-leave, | |
.v-enter-to { | |
opacity: 1; | |
} | |
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style lang="scss"> | |
.v-enter-from, | |
.v-leave-to { | |
opacity: 0; | |
} | |
.v-leave-from, | |
.v-enter-to { | |
opacity: 1; | |
} | |
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vue from 'vue'; | |
import App from 'views/app/app.vue' | |
new Vue({ | |
router, | |
render: h => h(App) | |
}).$mount('#app'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createApp } from 'vue'; | |
import App from 'views/app/app.vue' | |
const app = createApp(App); | |
app.mount('#app'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import somePlugin from 'plugins/some-plugin' | |
import someDirective from 'directives/some-directive' | |
Vue.use(somePlugin); | |
Vue.directive('some-directive', someDirective) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import somePlugin from 'plugins/some-plugin' | |
import someDirective from 'directives/some-directive' | |
app.use(somePlugin); | |
app.directive('some-directive', someDirective) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
import Vue from 'vue' | |
export default Vue.extend({ | |
// | |
}) | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
import { defineComponent } from 'vue' | |
export default defineComponent({ | |
// | |
}) | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Vue.prototype.$title = 'Some title' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.config.globalProperties.$title = 'Some title' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// set | |
Vue.set(state.moduleName, 'keyName', 'value') | |
vm.$set(state.moduleName, 'keyName', 'value') | |
// delete | |
Vue.delete(state.moduleName, 'keyName', 'value') | |
vm.$delete(state.moduleName, 'keyName', 'value') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// set | |
Object.assign(state.moduleName, { keyName: 'value' }) | |
// delete | |
delete state.moduleName.keyName |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
this.$nextTick(() => { | |
// something DOM-related | |
}) | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
import { nextTick } from 'vue' | |
nextTick(() => { | |
// something DOM-related | |
}) | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
export default { | |
beforeDestroy() { | |
console.log('Before component unmounts!') | |
}, | |
destroyed() { | |
console.log('Component unmounted!') | |
}, | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
export default { | |
beforeUnmount() { | |
console.log('Before component unmounts!') | |
}, | |
unmounted() { | |
console.log('Component unmounted!') | |
}, | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
export default { | |
emits: ['click'], | |
methods: { | |
onTitleClick(title) { | |
this.$emit('click', title) | |
} | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<MyComponent @hook:mounted="foo"> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<MyComponent @vnode-mounted="foo"> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<CustomComponent v-model="title" /> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<CustomComponent v-model:value="title" /> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
export default { | |
methods: { | |
updateTitle() { | |
this.$emit('input') | |
} | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
export default { | |
// Don't forget to list the event in the 'emits' option | |
emits: ['update:modelValue'], | |
methods: { | |
updateTitle() { | |
this.$emit('update:modelValue') | |
} | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<ChildComponent v-model="active" :title.sync="pageTitle" /> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<ChildComponent v-model="active" v-model:title="pageTitle" /> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MyDirective = { | |
created(el, binding, vnode, prevVnode) {}, | |
beforeMount() {}, | |
mounted() {}, | |
beforeUpdate() {}, | |
updated() {}, | |
beforeUnmount() {}, | |
unmounted() {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bind(el, binding, vnode) { | |
const vm = vnode.context | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mounted(el, binding, vnode) { | |
const vm = binding.instance | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<p>{{ user.lastName | uppercase }}</p> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<p>{{ uppercasedLastName }}</p> | |
<template> | |
<script lang="ts"> | |
export default { | |
computed: { | |
uppercasedLastName(): string { | |
return this.user.lastName.toUpperCase() | |
} | |
}, | |
// or alternatively | |
// {{ uppercaseText(user.lastName) }} | |
methods: { | |
uppercaseText(text: string) { | |
return text.toUpperCase() | |
} | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<button is="someComponent" /> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<component is="someComponent" /> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// e.g. in app.ts | |
import mitt from 'mitt' | |
const emitter = mitt() | |
app.config.globalProperties.$emitter = emitter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.$on('clickAway', this.hidePopover) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.$emitter.on('clickAway', this.hidePopover) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<SpecialButton v-on:click.native="foo" /> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<SpecialButton v-on:click="foo" /> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<CustomComponent>Hello World</CustomComponent> | |
</template> | |
<script lang="ts"> | |
export default { | |
mounted() { | |
console.log(this.$children[0]) | |
}, | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<CustomComponent ref="greeting">Hello World</CustomComponent> | |
</template> | |
<script lang="ts"> | |
export default { | |
mounted() { | |
console.log(this.$refs.greeting) | |
}, | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
export default { | |
mounted() { | |
console.log(this.$listeners) | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
export default { | |
mounted() { | |
console.log(this.$attrs.onClick) | |
console.log(this.$attrs.onMouseenter) | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
export default { | |
watch: { | |
someList: { | |
deep: true, | |
handler(val, oldVal) { | |
console.log('list was updated') | |
}, | |
}, | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MyComponent = { | |
component: () => import('./MyComponent.vue'), | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { defineAsyncComponent } from 'vue' | |
const MyComponent = defineAsyncComponent({ | |
loader: () => import('./MyComponent.vue'), | |
// ... | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<p :alt="false">Hello</p> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<p :alt="null">Hello</p> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default new Vuex.Store<IRootState>(storeOptions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default createStore(storeOptions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Router from 'vue-router' | |
const router = new Router({ | |
// ... | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createRouter } from 'vue-router' | |
const router = createRouter({ | |
// ... | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createRouter, createWebHistory } from 'vue-router' | |
// there is also createWebHashHistory and createMemoryHistory | |
createRouter({ | |
history: createWebHistory(), | |
routes: [], | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<router-link | |
:tag="info.id ? 'a' : 'div'" | |
:to="info.id ? { name: 'article', params: { id: info.id } } : {}" | |
> | |
{{ info.title }} | |
</router-link> | |
<template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<router-link | |
v-if="info.id" | |
:to="{ name: 'article', params: { id: info.id } }" | |
> | |
{{ info.title }} | |
</router-link> | |
<div v-else>{{ info.title }}</div> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vuex-shim.d.ts | |
import { ComponentCustomProperties } from 'vue' | |
import { Store } from 'vuex' | |
// Your own store state | |
interface IMyState { | |
count: number | |
} | |
declare module '@vue/runtime-core' { | |
interface ComponentCustomProperties { | |
$store: Store<IMyState> | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare module '*.vue' { | |
import Vue from 'vue' | |
export default Vue | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare module '*.vue' { | |
import { defineComponent } from 'vue' | |
const component: ReturnType<typeof defineComponent> | |
export default component | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare module 'vue/types/vue' { | |
export interface Vue { | |
$http: typeof axios | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare module '@vue/runtime-core' { | |
interface ComponentCustomProperties { | |
$http: typeof axios | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
export default { | |
props: { | |
modelValue: String // previously was `value: String` | |
}, | |
emits: ['update:modelValue'], | |
methods: { | |
changeTitle(title) { | |
this.$emit('update:modelValue', title) | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment