赞
踩
## Vue3总结 ### 官方文档 * [Vue3](https://v3.cn.vuejs.org/api/options*data.html) * [Vue2](https://vuejs.bootcss.com/api/) ### Vue3相对于Vue2的语法特性 #### 1.获取数据 * vue2 ```javascript export default { data() { return { name: 'myName', } }, mounted() { console.log(this.name) } }
import {ref} from 'vue'
export default {
setup() {
const name = ref('myName')
console.log(name.value)
return {name}
}
}
export default {
methods: {
show() {
console.log('show方法被调用')
}
},
mounted() {
this.show()
}
}
import {onMounted} from 'vue'
export default {
setup() {
function show() {
console.log('show方法被调用')
}
onMounted(() => {
show()
})
return {show}
}
}
export default {
methods: {
change() {
this.$emit('valueChange', 3)
}
},
}
import {onMounted} from 'vue'
export default {
setup(props, context) {
function change() {
context.emit('valueChange', 3)
}
}
}
export default {
mounted() {
console.log(this.$store.state.name)
this.$store.commit('show')
}
}
import {onMounted} from 'vue'
import {useStore} from 'vuex'
export default {
setup(props, context) {
const store = useStore()
onMounted(() => {
console.log(store.name)
store.commit('show')
})
}
}
<template> // el当前元素,divs是存储每个元素的数组 <div v-for="(item, index) in list" :ref="el => { divs[index] = el }">{{ item }}</div> </template> <script> import {onMounted, ref} from 'vue'; export default { setup() { const divs = ref([]); onMounted(() => { console.log(divs.value) }); return { divs }; }, }; </script>
<template>
<div class='form-element'>
<h2>{{ title }}</h2>
</div>
</template>
<template>
<div class='form-element'></div>
<h2> {{ title }}</h2>
</template>
export default { props: { title: String }, data () { return { username: '', password: '' } }, methods: { login () { // 登陆方法 } }, components:{ "buttonComponent":btnComponent }, computed:{ lowerCaseUsername(){ return this.username.toLowerCase(); } } }
<template> <div> <h2> {{ state.username }} </h2> </div> </template> <script> import { reactive } from 'vue' export default { props: { title: String }, setup () { const state = reactive({ //数据 username: '', password: '', lowerCaseUsername: computed(() => state.username.toLowerCase()) //计算属性 }) //方法 const login = () => { // 登陆方法 } return { login, state } } } </script>
Vue2--------------vue3
beforeCreate -> setup()
created -> setup()
beforeMount -> onBeforeMount
mounted -> onMounted
beforeUpdate -> onBeforeUpdate
updated -> onUpdated
beforeDestroy -> onBeforeUnmount
destroyed -> onUnmounted
activated -> onActivated
deactivated -> onDeactivated
import { toRefs } from 'vue'
setup(props, { attrs, slots, emit }) {
const { title } = toRefs(props)
console.log(title.value)
onMounted(() => {
console.log('title: ' + props.title)
})
}
<template> <teleport to="#modal"> <div id="center" v-if="isOpen"> <h2><slot>this is a modal</slot></h2> <button @click="buttonClick">Close</button> </div> </teleport> </template> <script> export default { props: { isOpen: Boolean, }, emits: { 'close-modal': null }, setup(props, context) { const buttonClick = () => { context.emit('close-modal') } return { buttonClick } } } </script> <style> #center { width: 200px; height: 200px; border: 2px solid black; background: white; position: fixed; left: 50%; top: 50%; margin-left: -100px; margin-top: -100px; } </style>
<template> <div id="app"> <button @click="openModal">Open Modal</button><br/> <modal :isOpen="modalIsOpen" @close-modal="onModalClose"> My Modal !!!!</modal> </div> </template> <script> import Modal from './components/Modal.vue' import{ref} from 'vue' export default { name: 'App', components: { Modal }, setup() { const modalIsOpen = ref(false) const openModal = () => { modalIsOpen.value = true } const onModalClose = () => { modalIsOpen.value = false } return { modalIsOpen, openModal, onModalClose } } } </script> <style> #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } </style>
<body>
<div id="app"></div>
<div id="modal"></div>
</body>
<template>
<div ref="mainContainer" ></div>
</template>
<script>
setup() {
const mainContainer = ref(null)
onMounted(()=>{console.log(mainContainer)})
return {
mainContainer,
}
}
</script>
import { resolveComponent } from 'vue' // 需先引入 resolveComponent cloums中写法render写法 { title: '操作', align: 'center', width: 100, fixed: 'right', key: 'couTypeCategoryName', render: (h, params) => { return h(resolveComponent('el-switch'), { modelValue: params.row.isShow, 'active-value': 1, 'inactive-value': 0, // 'onUpdate:modelValue': value => params.row.state = value onChange: (val) => { }, }) } },
render: (h, params) => {
return h(resolveComponent('el-input'), {
size: 'small',
modelValue: params.row.comments,
'onUpdate:modelValue': (value) => (params.row.comments = value),
})
},
<template #footer>
<span class="dialog-footer">
<el-button>取消</el-button>
<el-button type="primary">确定</el-button>
</span>
</template>
在vue3中被移除
###Fragment
vue2每个模版必须有一个根节点
vue3可以有多个根节点
<!--vue2-->
<template>
<div>
<span></span>
<span></span>
</div>
</template>
<!--vue3-->
<template>
<span>hello</span>
<span>world</span>
</template>
###v-if v-for 优先级
vue2 时 v-for 优先级高,所以v-for 和v-if一起会耗性能,增加了喝多不必要的判断
vue3 v-if比v-for有更高的优先级
###vVNode Prop
vue2和vue3的虚拟dom参数有所区别,vue3更加扁平化
<!--vue2--> render: (h, params) => { let btnArr = [ h( 'Button', { props: { type: 'primary', }, style: { marginLeft: '8px', }, on: { click: () => { this.modalType = 'detail' this.viewType = 2 this.item = params.row this.pageStatus = true this.pState = true }, }, }, '查看详情' ), ] return h('div', btnArr) }, <!--vue3--> render:(h,params)=>{ return h(resolveComponent('el-input-number'), { type: 'number', size: 'large', modelValue:params.row.value, stepStrictly:true, controls:false, min:"1", 'onUpdate:modelValue':(value) => { params.row.value = parseInt(value)}, style: { width:'200px'}, }) }
###ref和reactive 响应式的基础
接受一个参数,返回一个响应式数据
不同的是ref一般处理基础数据类型,reactive一般处理引用数据类型
使用 reactive,toRefs 保证 reactive 对象属性保持响应性
import { ref, computed, reactive, toRefs } from 'vue'
setup() {
const DataProps = reactive({
count: 0,
increase: () => { data.count++},
double: computed(() => data.count * 2)
})
const refData = toRefs(data)
return {
...refData
}
}
###v-model
vue2只能绑定一个,vue3可以绑定多个
<!--父组件--> <template> // v-model:modelValue简写为v-model // 可绑定多个v-model <child v-model="state.name" v-model:age="state.age" /> </template> <script setup> import { reactive } from 'vue' // 引入子组件 import child from './child.vue' const state = reactive({ name: 'Jerry', age: 20 }) </script> 子组件 <template> <span @click="changeInfo">我叫{{ modelValue }},今年{{ age }}岁</span> </template> <script setup> // import { defineEmits, defineProps } from 'vue' // defineEmits和defineProps在<script setup>中自动可用,无需导入 // 需在.eslintrc.js文件中【globals】下配置【defineEmits: true】、【defineProps: true】 defineProps({ modelValue: String, age: Number }) const emit = defineEmits(['update:modelValue', 'update:age']) const changeInfo = () => { // 触发父组件值更新 emit('update:modelValue', 'Tom') emit('update:age', 30) } </script>
###defineComponent函数,只是对setup函数进行封装,返回options的对象;
###父组件获取子组件的属性和方法
<!--父组件--> <template> <child ref="comp"></child> <button @click="handlerClick">按钮</button> </template> <script setup> import child from "./comp/expose.vue" import { ref } from "vue" const comp = ref(null) const handlerClick = () => { console.log(comp.value.a) // 获取子组件对外暴露的属性 comp.value.someMethod() // 调用子组件对外暴露的方法 } </script> <!--子组件--> <template> <div>{{a}}</div> <button @click='someMethod'>按钮子</button> </template> <script setup> import { ref } from 'vue' let someMethod=()=>{ console.log(666) } const a = 1 const b = ref(2) defineExpose({ a, b, someMethod }) </script>
###其他
1.vue2 defineProperty只能监听某个属性,不能对全对象监听
2 vue3 Proxy 代理,可以检测到数组内部数据的变化
3.vue3 Teleport瞬移组件
4.Vue2 - 这里把数据放入data属性中
5.vue 3 Composition API
需要使用一个新的setup()方法,此方法在组件初始化构造的时候触发。
从vue引入reactive
使用reactive()方法来声名我们的数据为响应性数据
使用setup()方法来返回我们的响应性数据,从而我们的template可以获取这些响应性数据
vue3Composition API生命后期
setup() :开始创建组件之前,在beforeCreate和created之前执行。创建的是data和method
onBeforeMount() : 组件挂载到节点上之前执行的函数。
onMounted() : 组件挂载完成后执行的函数。
onBeforeUpdate(): 组件更新之前执行的函数。
onUpdated(): 组件更新完成之后执行的函数。
onBeforeUnmount(): 组件卸载之前执行的函数。
onUnmounted(): 组件卸载完成后执行的函数
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。