带你了解vue3组合式api基本写法

news/2024/10/20 15:45:09/

本文的目的,是为了让已经有 Vue2 开发经验的 人 ,快速掌握 Vue3 的写法。
因此, 本篇假定你已经掌握 Vue 的核心内容 ,只为你介绍编写 Vue3 代码,需要了解的内容。
一、Vue3 里 script 的三种写法
首先,Vue3 新增了一个叫做组合式 api 的东西,英文名叫 Composition API。因此 Vue3 的 script 现在支持三种写法,

文章目录

  • 1、data——唯一需要注意的地方
  • 2、methods
  • 3、props
  • 4、emits 事件
  • 5、computed
  • 6、watch
  • 7、生命周期

1、最基本的 Vue2 写法

<template><div>{{ count }}</div><button @click="onClick">增加 1</button>
</template><script>
export default {data() {return {count: 1,};},methods: {onClick() {this.count += 1;},},
}
</script>

2、setup() 属性

<template><div>{{ count }}</div><button @click="onClick">增加 1</button>
</template><script>
import { ref } from 'vue';
export default {// 注意这部分setup() {let count = ref(1);const onClick = () => {count.value += 1;};return {count,onClick,};},}
</script>

3、

<template><div>{{ count }}</div><button @click="onClick">增加 1</button>
</template><script setup>
import { ref } from 'vue';const count = ref(1);
const onClick = () => {count.value += 1;
};
</script>

正如你看到的那样,无论是代码行数,还是代码的精简度,

1、data——唯一需要注意的地方

整个 data 这一部分的内容,你只需要记住下面这一点。
以前在 data 中创建的属性,现在全都用 ref() 声明。
在 template 中直接用,在 script 中记得加 .value 。
在开头,我就已经写了一个简单的例子,我们直接拿过来做对比。
1)写法对比
// Vue2 的写法

<template><div>{{ count }}</div><button @click="onClick">增加 1</button>
</template><script>
export default {data() {return {count: 1,};},methods: {onClick() {this.count += 1;},},
}
</script>

// Vue3 的写法

<template><div>{{ count }}</div><button @click="onClick">增加 1</button>
</template><script setup>
import { ref } from 'vue';// 用这种方式声明
const count = ref(1);const onClick = () => {// 使用的时候记得 .valuecount.value += 1;
};
</script>

2)注意事项——组合式 api 的心智负担
a、ref 和 reactive
Vue3 里,还提供了一个叫做 reactive 的 api。
但是我的建议是,你不需要关心它。绝大多数场景下,ref 都够用了。
b、什么时候用 ref() 包裹,什么时候不用。
要不要用ref,就看你的这个变量的值改变了以后,页面要不要跟着变。
当然,你可以完全不需要关心这一点,跟过去写 data 一样就行。
只不过这样做,你在使用的时候,需要一直 .value。
c、不要解构使用
在使用时,不要像下面这样去写,会丢失响应性。
也就是会出现更新了值,但是页面没有更新的情况
// Vue3 的写法

<template><div>{{ count }}</div><button @click="onClick">增加 1</button>
</template><script setup>
import { ref } from 'vue';const count = ref(1);
const onClick = () => {// 不要这样写!!const { value } = count;value += 1;
};
</script>

注意: 学习 Vue3 就需要考虑像这样的内容,徒增了学习成本。实际上这些心智负担,在学习的过程中,是可以完全不需要考虑的。
这也是为什么我推荐新人先学习 Vue2 的写法。

2、methods

声明事件方法,我们只需要在 script 标签里,创建一个方法对象即可。
剩下的在 Vue2 里是怎么写的,Vue3 是同样的写法。
// Vue2 的写法

<template><div @click="onClick">这是一个div</div>
</template><script>
export default {methods: {onClick() {console.log('clicked')},},
}
</script>
复制代码
// Vue3 的写法
<template><div @click="onClick">这是一个div</div>
</template><script setup>// 注意这部分
const onClick = () => {console.log('clicked')
}</script>

3、props

声明 props 我们可以用 defineProps(),具体写法,我们看代码。
1)写法对比
// Vue2 的写法

<template><div>{{ foo }}</div>
</template><script>
export default {props: {foo: String,},created() {console.log(this.foo);},
}
</script>

// Vue3 的写法

<template><div>{{ foo }}</div>
</template><script setup>// 注意这里
const props = defineProps({foo: String
})// 在 script 标签里使用
console.log(props.foo)
</script>

2)注意事项——组合式 api 的心智负担
使用 props 时,同样注意不要使用解构的方式。

<script setup>
const props = defineProps({foo: String
})// 不要这样写
const { foo } = props;
console.log(foo)
</script>

4、emits 事件

与 props 相同,声明 emits 我们可以用 defineEmits(),具体写法,我们看代码。
// Vue2 的写法

<template><div @click="onClick">这是一个div</div>
</template><script>
export default {emits: ['click'], // 注意这里methods: {onClick() {this.$emit('click'); // 注意这里},},}
</script>

// Vue3 的写法

<template><div @click="onClick">这是一个div</div>
</template><script setup>// 注意这里
const emit = defineEmits(['click']);const onClick = () => {emit('click') // 注意这里
}</script>

5、computed

直接上写法对比。
// Vue2 的写法

<template><div><span>{{ value }}</span><span>{{ reversedValue }}</span></div>
</template><script>
export default {data() {return {value: 'this is a value',};},computed: {reversedValue() {return value.split('').reverse().join('');},},
}
</script>

// Vue3 的写法

<template><div><span>{{ value }}</span><span>{{ reversedValue }}</span></div>
</template><script setup>
import {ref, computed} from 'vue'
const value = ref('this is a value')// 注意这里
const reversedValue = computed(() => {// 使用 ref 需要 .valuereturn value.value.split('').reverse().join('');
})</script>

6、watch

这一部分,我们需要注意一下了,Vue3 中,watch 有两种写法。一种是直接使用 watch,还有一种是使用 watchEffect。
两种写法的区别是:

watch 需要你明确指定依赖的变量,才能做到监听效果。

而 watchEffect 会根据你使用的变量,自动的实现监听效果。

1)直接使用 watch
// Vue2 的写法

<template><div>{{ count }}</div><div>{{ anotherCount }}</div><button @click="onClick">增加 1</button>
</template><script>
export default {data() {return {  count: 1,anotherCount: 0,};},methods: {onClick() {this.count += 1;},},watch: {count(newValue) {this.anotherCount = newValue - 1;},},
}
</script>

// Vue3 的写法

<template><div>{{ count }}</div><div>{{ anotherCount }}</div><button @click="onClick">增加 1</button>
</template><script setup>
import { ref, watch } from 'vue';const count = ref(1);
const onClick = () => {count.value += 1;
};const anotherCount = ref(0);// 注意这里
// 需要在这里,
// 明确指定依赖的是 count 这个变量
watch(count, (newValue) => {anotherCount.value = newValue - 1;
})</script>

2)使用 watchEffect
// Vue2 的写法

<template><div>{{ count }}</div><div>{{ anotherCount }}</div><button @click="onClick">增加 1</button>
</template><script>
export default {data() {return {  count: 1,anotherCount: 0,};},methods: {onClick() {this.count += 1;},},watch: {count(newValue) {this.anotherCount = newValue - 1;},},
}
</script>

// Vue3 的写法

<template><div>{{ count }}</div><div>{{ anotherCount }}</div><button @click="onClick">增加 1</button>
</template><script setup>
import { ref, watchEffect } from 'vue';const count = ref(1);
const onClick = () => {count.value += 1;
};const anotherCount = ref(0);// 注意这里
watchEffect(() => {// 会自动根据 count.value 的变化,// 触发下面的操作anotherCount.value = count.value - 1;
})</script>

7、生命周期

Vue3 里,除了将两个 destroy 相关的钩子,改成了 unmount,剩下的需要注意的,就是在

<template><div></div>
</template><script>
export default {beforeCreate() {},created() {},beforeMount() {},mounted() {},beforeUpdate() {},updated() {},// Vue2 里叫 beforeDestroybeforeUnmount() {},// Vue2 里叫 destroyedunmounted() {},// 其他钩子不常用,所以不列了。
}
</script>

// 组合式 api 写法

<template><div></div>
</template><script setup>
import {onBeforeMount,onMounted,onBeforeUpdate,onUpdated,onBeforeUnmount,onUnmounted,
} from 'vue'onBeforeMount(() => {})
onMounted(() => {})onBeforeUpdate(() => {})
onUpdated(() => {})onBeforeUnmount(() => {})
onUnmounted(() => {})
</script>

三、结语
好了,对于快速上手 Vue3 来说,以上内容基本已经足够了。
这篇文章本身不能做到帮你理解所有 Vue3 的内容,但是能帮你快速掌握 Vue3 的写法。
如果想做到对 Vue3 的整个内容心里有数,还需要你自己多看看 Vue3 的官方文档。


http://www.ppmy.cn/news/53860.html

相关文章

Linux crontab计划任务和at一次性计划任务

文章目录 一、crontab常用命令二、计划任务书写格式例子&#xff1a;问题1&#xff1a;每月1&#xff0c;10&#xff0c;22号的4:45重启network服务问题2&#xff1a;每周六周日的1&#xff1a;10重启network服务问题3&#xff1a;每天18&#xff1a;00至23:00之间每隔30分钟重…

appuploader 常规使用登录方法

转载&#xff1a;登录appuploader 登录appuploader 常规使用登录方法 双击appuploader.exe 启动appuploader 点击底部的未登录&#xff0c;弹出登录框 在登录框内输入apple开发者账号 如果没有apple开发者账号&#xff0c;只是普通的apple账号&#xff0c;请勾选上未支付688…

基于JavaSpringboot+vue国风汉服文化交流宣传系统

基于JavaSpringbootvue国风汉服文化交流宣传系统 博主介绍&#xff1a;5年java开发经验&#xff0c;专注Java开发、定制、远程、指导等,csdn特邀作者、专注于Java技术领域 作者主页 超级帅帅吴 Java项目精品实战案例《500套》 欢迎点赞 收藏 ⭐留言 文末获取源码联系方式 文章目…

不停机修复mysql主从数据同步错误导致服务器磁盘占满问题

事情的现象&#xff1a; 线上生产环境mysql服务器采用主从结构。突然告警从库服务器磁盘占用高。经过磁盘空间检查&#xff0c;主要是/mysql/data目录使用100%&#xff08;直接占满了&#xff09;&#xff0c;进入目录后发现被文件slave-relay-bin.*系列文件占满了。常理数据不…

React18开发总结(完善中)

遇到这样一个问题&#xff0c;初始化时用户登陆后需要获取到用户信息&#xff0c;但是发现获取用户信息这个接口触发了2次&#xff0c;这是不应该的&#xff0c;于是我查阅了一下资料&#xff0c;把自己的笔记记录下来。 还有就是使用mobx遇到的控制台警告问题&#xff0c;也一…

计算机组成原理---第 6 章总线系统

一、总线的概念和结构形态 总线的基本概念 ⑴概述 总线是构成计算机系统的互联机构&#xff0c;是多个系统功能部件之间进行数据传送的公共通路。 ⑵ 分类 总线的分类方式有很多&#xff1a;如被分为外部总线和内部总线、系统总线和非系统总线、片内总线和PCB板级总线、串行总…

【Unity3D插件】Embedded Browser嵌入式浏览器插件使用教程

推荐阅读 CSDN主页GitHub开源地址Unity3D插件分享简书地址我的个人博客 大家好&#xff0c;我是佛系工程师☆恬静的小魔龙☆&#xff0c;不定时更新Unity开发技巧&#xff0c;觉得有用记得一键三连哦。 一、前言 好久没有介绍插件了&#xff0c;今天分享一款比较好用的嵌入式…

@RequestBody和@RequestParam和@PathVariable

RequestBody 写着写着忘记了mvc这几个常用的注解的作用&#xff0c;一时有点凝噎。 为什么get请求不用加RequestBody也能接收到参数&#xff0c;post请求不加RequestBody就接收不到参数&#xff1f; 这是因为HTTP协议规定了请求方式对应的数据传递方式不同。对于GET请求&#x…