你准备好迎接vue3.0了吗

2020-06-16 06:42:46易采站长站整理

生命周期

vue3.0中取消了 beforeCreate 和 created 两个周期,因为setup会在这个这个周期前执行,因此你可以在setup中进行你需要的处理。其他的生命周期全部以on开头。


import {
onBeforeMount,
onMounted,
onBeforeUnmount,
onBeforeUpdate,
onDeactivated,
onUnmounted,
onUpdated
} from '@vue/composition-api'
export default {
setup(){
onBeforeMount(() =>{
console.log('onBeforeMount');
})
onMounted(() =>{
console.log('onMounted');
})
onBeforeUnmount(() =>{
console.log('onBeforeUnmount');
})
onBeforeUpdate(() =>{
console.log('onBeforeUpdate');
})
onDeactivated(() =>{
console.log('onDeactivated');
})
onUnmounted(() =>{
console.log('onUnmounted');
})
onUpdated(() =>{
console.log('onUpdated');
})
}
}

Mixin

vue3.0中使用函数式的API代替原有的mixin,mixin很容易引起命名重合和覆盖引入mixin的页面属性。

在vue3.0中以一个API的形式存在,当你需要时,将其引入。直接看例子

mixin.vue


<template>
<div>
<h3>mixins</h3>
<div>
<el-input v-model="searchValue" type="text" @change="onSearch"/>
<div>
<ul>
<li v-for="name in names" :key="name.id" v-show="name.show">
{{name.value}}
</li>
</ul>
</div>
</div>
</div>
</template>

<script>
import searchName from './searchName.js'
export default {
setup(){
let list = [
{id: 1, value: 'vue', show: true},
{id: 2, value: 'react', show: true},
{id: 3, value: 'angular', show: true},
{id: 4, value: 'elementui', show: true},
{id: 5, value: 'ant', show: true},
{id: 6, value: 'javascript', show: true},
{id: 7, value: 'css', show: true},
] var {onSearch, names, searchValue} = searchName(list)
return {
onSearch,
names,
searchValue
}
}
}
</script>
<style>
</style>

searchName.js


import {reactive, toRefs} from '@vue/composition-api'

export default function searchName(names){
const state = reactive({
names: names,
searchValue: ''
})
const onSearch = () => {
state.names.forEach(name => {