vue2-migration-helper
v0.3.12
Published
Updates vue2 sfc components to vue3 composition api syntax.
Downloads
25
Maintainers
Readme
vue2-migration-helper
Transforms Vue.js SFCs to composition api syntax.
Install
npm i vue2-migration-helper
CLI
# convert all .vue files in source directory and outputs in target directory
vue2-migration-helper --source="source" --target="target"
# displays help
vue2-migration-helper --help
Programatically use
import { vue2MigrationHelper } from 'vue2-migration-helper'
vue2MigrationHelper({
source: 'source/',
target: 'target/',
})
Features
- [x] add
setup
method- with
props
andcontext
arguments
- with
- [x] add required vue
imports
- only adds required imports after traversing code
- [x] update
data
properties- Uses
data
variable usingreactive
- Return reactive references using
ref
- updates usage of these varaiables
- Uses
- [x] update
computed
syntax- defines variable for each property using new syntax
computed
- defines variable for each property using new syntax
- [x] update
watch
syntax- updates watch syntax
- [x] integrate
methods
directly into setup- defines
methods
into the setup body - updates method calls
- defines
- [x] update template
ref
usage- adds a new variable for each
templateRef
usingref(null)
- add new defined templateRefs to return statement
- adds a new variable for each
- [x] convert
props
syntax - [x] update
lifecycle
hooks and remove deperecated lifecycle hooks- removes depracted life cycle hooks, injects deprecated hooks code into
setup
method. - copies other
hooks
into thesetup
method
- removes depracted life cycle hooks, injects deprecated hooks code into
- [x]
component
registration - [x] replace
this
usage with newcontext
parameter for $events etc- replaces
this
keyword usage as this no longer refers to vue component itself.
- replaces
missing something?
Example
For a Vue.js SFC (single file component) like this:
import SomeComponent from './SomeComponent'
const zero = {}
export default {
props: {
title: String,
likes: Number,
callback: Function,
},
components: {
SomeComponent,
},
data() {
return {
one: true,
two: 2,
three: 'three',
}
},
watch: {
one(val) {
console.log(val)
},
two: (val) => {
console.log(val)
},
three: function (a, b) {
console.log(a, b)
},
},
computed: {
oneComputed() {
return !this.one
},
twoComputed: () => {
return !this.one
},
threeComputed: function () {
return !this.one
},
},
created() {
console.log('created')
},
mounted() {
console.log('mounted')
},
methods: {
...[
function fourMethod() {
console.log('fourMethod')
},
function fiveMethod() {
console.log('fiveMethod')
},
],
oneMethod() {
const html = this.$refs.templateRef.innerHTML
console.log('oneMethod')
console.log(this.oneComputed)
},
twoMethod: function () {
this.$refs.templateRef.innerHTML = 'html'
console.log('twoMethod')
console.log(this.twoComputed)
this.oneMethod()
console.log(this.$router)
},
threeMethod: () => {
console.log('threeMethod')
console.log(this.threeComputed)
this.twoMethod()
console.log(this.$store)
},
},
}
this script generates Vue SFC using composition API:
import {
ref,
reacted,
toRefs,
watch,
computed,
onCreated,
onMounted,
} from 'vue'
import SomeComponent from './SomeComponent'
const zero = {}
export default {
components: {
SomeComponent,
},
props: {
title: String,
likes: Number,
callback: Function,
},
setup(props, context) {
const data = reactive({
one: true,
two: 2,
three: 'three',
})
const templateRef = ref(null)
watch(three, (a, b) => {
console.log(a, b)
})
watch(two, (val) => {
console.log(val)
})
watch(one, (val) => {
console.log(val)
})
const oneComputed = computed(() => {
return !data.one
})
const twoComputed = computed(() => {
return data.two + 5
})
const threeComputed = computed(() => {
return data.three.toUpperCase()
})
;(() => {
console.log('created')
})()
onMounted(() => {
console.log('mounted')
})
function fourMethod() {
console.log('fourMethod')
}
function fiveMethod() {
console.log('fiveMethod')
}
function oneMethod() {
const html = templateRef.innerHTML
console.log('oneMethod')
console.log(oneComputed)
console.log(context.$data)
}
function twoMethod() {
templateRef.innerHTML = 'html'
console.log('twoMethod')
console.log(twoComputed)
oneMethod()
console.log(context.$router)
}
function threeMethod() {
console.log('threeMethod')
console.log(threeComputed)
twoMethod()
console.log(fourMethod)
console.log(context.$store)
}
return {
...ref(data),
oneComputed,
twoComputed,
threeComputed,
fourMethod,
fiveMethod,
oneMethod,
twoMethod,
threeMethod,
templateRef,
}
},
}