最近在尝试使用vue3,整理了一些和vue2在使用上的一些区别
vue2
的script
结构<template>
??<div>
??</div>
</template>
<script>
export?default?{
??name:?'',
??components:?{},
??props:?{},
??data()?{
????return?{}
??},
??watch:?{},
??created()?{},
??mounted()?{},
??methods:?{}
}
</script>
<style??lang="scss"?scoped></style>
vue3
的script
结构<template> </template>
<script lang="ts">
import { defineComponent, onMounted, reactive, UnwrapRef, watch } from 'vue';
interface State {}
export default defineComponent({
name: 'components name',
props: {},
setup(props) {
console.log('props: ', props);
//data
const state: UnwrapRef<State> = reactive({});
//Lifecycle Hooks
onMounted(() => {});
//watch
watch(
() => props,
(_count, _prevCount) => {},
{
deep: true,
immediate: true,
}
);
//methods
const getList = () => {};
return {
state,
getList
};
},
});
</script>
<style lang="scss" scoped></style>
因为 setup 是围绕 beforeCreate 和 created 生命周期钩子运行的,所以不需要显式地定义它们。换句话说,在这些钩子中编写的任何代码都应该直接在 setup 函数中编写。
下表包含如何在 setup ()
内部调用生命周期钩子:
选项式 API | Hook inside setup |
---|---|
beforeCreate | Not needed* |
created | Not needed* |
beforeMount | onBeforeMount |
mounted | onMounted |
beforeUpdate | onBeforeUpdate |
updated | onUpdated |
beforeUnmount | onBeforeUnmount |
unmounted | onUnmounted |
errorCaptured | onErrorCaptured |
renderTracked | onRenderTracked |
renderTriggered | onRenderTriggered |
//import { ref } from 'vue';
const count = ref(0)
console.log(count.value) // 0
count.value++
console.log(count.value) // 1
变量的声明有点类似于react
的State Hook
推荐使用reactive包裹数组,
//import { reactive } from 'vue';
const state = reactive({
arr: []
});
state.arr = [1, 2, 3]
或者
const state = ref([])
state.value = [1, 2, 3]
或者
const arr = reactive([])
arr.push(...[1, 2, 3])
这几种办法都可以触发响应性,然后界面中正常使用v-for
即可,推荐第一种
父子组件传值的写法
<Search @searchData="searchData" :quaryParams="quaryParams"/>
父组件的写法和vue
还是一样的,只是子组件需要作一些改变
<script lang="ts">
import { defineComponent } from 'vue';
interface GetUserListParams {
pageNum: number;
pageSize: number;
roleName: string;
}
export default defineComponent({
name: 'Search',
props: {
quaryParams: {
type: Object as PropType<GetUserListParams> ,
default: () = > ({
pageNum: 1,
pageSize: 10,
roleName: ''
})
}
},
emits: ['searchData'],//需要声明emits
setup(_props, context) {
const onSubmit = () => {
context.emit('searchData', "我是子节点传递给父节点的值");
}
return {
getData
}
}
});
</script>
vue2
写法<!-- src/components/MyMap.vue -->
<template>
<MyMarker />
</template>
<script>
import MyMarker from './MyMarker.vue'
export default {
components: {
MyMarker
},
provide: {
location: 'North Pole',
geolocation: {
longitude: 90,
latitude: 135
}
}
}
</script>
<!-- src/components/MyMarker.vue -->
<script>
export default {
inject: ['location', 'geolocation']
}
</script>
vue3
写法<!-- src/components/MyMap.vue -->
<template>
<MyMarker />
</template>
<script>
import { provide, reactive, ref } from 'vue'
import MyMarker from './MyMarker.vue
export default {
components: {
MyMarker
},
setup() {
const location = ref('North Pole')
const geolocation = reactive({
longitude: 90,
latitude: 135
})
provide('location', location)
provide('geolocation', geolocation)
}
}
</script>
<!-- src/components/MyMarker.vue -->
<script>
import { inject } from 'vue'
export default {
setup() {
const userLocation = inject('location', 'The Universe')
const userGeolocation = inject('geolocation')
return {
userLocation,
userGeolocation
}
}
}
</script>
更多可阅读Provide / Inject
watch: {
count: {
handler: function(val, oldval) {},
immediate: true,
deep: true
}
}
setup() {
const count = ref(0)
//监听count
watch(
() = > count, (_count, _prevCount) = > {}, {
deep: true,
immediate: true
}
);
}
后续遇到其他问题慢慢补充
本文是介绍 RecyclerView 入门基础 系列文章 的第四篇。如果您已经对创建 Recycl...
在CSS中,根据元素显示模式的不同元素标签被分为了两类:行内元素(inline-level...
打开纯文本代码链接时,自动高亮代码,支持191种语言,97个主题 特性 支持191种...
前言: 不知多久能学会 Elisp 上一章: 动态模块 从本章开始,进入这份 Elisp 教...
为表达全国各族人民对抗击新冠肺炎疫情斗争牺牲烈士和逝世通报的深切哀悼,国务...
微信搜索【 脑子进煎鱼了 】关注这一只爆肝煎鱼。本文 GitHub github.com/eddycj...
前言 在制作顶部菜单的时候,都会要求制作弹出的二级菜单,早先的做法是用jQuery...
【内容】: 1.利用background-image 渐变样式 2.可以利用scale缩放 3.给伪元素设...
简介 处理大量并发是 Go 语言的一大优势。语言内置了方便的并发语法,可以非常方...
在HTML5中,我们可以使用drawImage方法在canvas上进行画图操作,其基本代码如下...