vue3 学习之路1
setup函数
- 使用Composition API的入口
- 在beforeCreate之前调用
- 在setup中没有this
ref函数
- 返回一个响应式引用
- name是一个响应式对象
<template><div id="app"><p>{{ name }}</p><p>{{ age }}<button type="button" @click="plusOne()">+</button></p></div>
</template><script>
import {ref} from 'vue'export default {setup() {const name = ref('hooli')const age = ref(18)function plusOne() {age.value++}return {name, age, plusOne}}
}
</script>
计算属性
- 没使用计算属性,单纯响应式
<template><div id="app"><p>{{ name }}</p><p><button type="button" @click="changeAge(-1)">-</button>{{ age }}<button type="button" @click="changeAge(1)">+</button></p><p>出生年份: {{ 2021 - age }}</p></div>
</template><script>
import {ref} from 'vue'export default {setup() {const name = ref('hooli')const age = ref(18)function changeAge(val) {age.value += val}return {name, age, changeAge}}
}
</script>
- 使用计算属性
<template><div id="app"><p>{{ name }}</p><p><button type="button" @click="changeAge(-1)">-</button>{{ age }}<button type="button" @click="changeAge(1)">+</button></p><p>出生年份: {{ year }}</p></div>
</template><script>
import {ref, computed} from 'vue'export default {setup() {const name = ref('hooli')const age = ref(18)const year = computed(() => {return 2021 - age.value})function changeAge(val) {age.value += val}return {name, age, changeAge, year}}
}
</script>
- 计算属性,getter && setter
<template><div id="app"><p>{{ name }}</p><p><button type="button" @click="changeAge(-1)">-</button>{{ age }}<button type="button" @click="changeAge(1)">+</button></p><p><button type="button" @click="changeYear(-1)">-</button>出生年份: {{ year }}<button type="button" @click="changeYear(1)">+</button></p></div>
</template><script>
import {ref, computed} from 'vue'export default {setup() {const name = ref('hooli')const age = ref(18)const year = computed({get: () => {return 2021 - age.value},set: val => {age.value = 2021 - val}})function changeAge(val) {age.value += val}function changeYear(val) {year.value = year.value + val}return {name, age, changeAge, year, changeYear}}
}
</script>
响应式对象 reactive
- 引入reactive,并创建reactive对象
<template><div id="app"><p>{{ data.name }}</p><p><button type="button" @click="changeAge(-1)">-</button>{{ data.age }}<button type="button" @click="changeAge(1)">+</button></p><p><button type="button" @click="changeYear(-1)">-</button>出生年份: {{ data.year }}<button type="button" @click="changeYear(1)">+</button></p></div>
</template><script>
import {computed, reactive} from 'vue'export default {setup() {const data = reactive({name: 'hooli',age: 18,year: computed({get: () => {return 2021 - data.age},set: val => {data.age = 2021 - val}})})function changeAge(val) {data.age += val}function changeYear(val) {data.year = data.year + val}return {data, changeAge, changeYear}}
}
</script>
- 如果页面想继续使用{{name}},需要引入toRefs将响应式对象转变成普通对象
<template><div id="app"><p>{{ name }}</p><p><button type="button" @click="changeAge(-1)">-</button>{{ age }}<button type="button" @click="changeAge(1)">+</button></p><p><button type="button" @click="changeYear(-1)">-</button>出生年份: {{ year }}<button type="button" @click="changeYear(1)">+</button></p></div>
</template><script>
import {computed, reactive, toRefs} from 'vue'export default {setup() {const data = reactive({name: 'hooli',age: 18,year: computed({get: () => {return 2021 - data.age},set: val => {data.age = 2021 - val}})})function changeAge(val) {data.age += val}function changeYear(val) {data.year = data.year + val}return {...toRefs(data), changeAge, changeYear}}
}
</script>
setup函数
- setup函数的参数:props, context
- context相当于this
import {computed, reactive, toRefs} from 'vue'export default {props: {title: String},setup(props, context) {const data = reactive({name: 'hooli',age: 18,year: computed({get: () => {return 2021 - data.age},set: val => {data.age = 2021 - val}})})function changeAge(val) {data.age += valconsole.log(props.title)context.emit('title-changed')}function changeYear(val) {data.year = data.year + val}return {...toRefs(data), changeAge, changeYear}}
}
watch
import {computed, reactive, toRefs, watch} from 'vue'export default {props: {title: String},setup(props, context) {const data = reactive({name: 'hooli',age: 18,year: computed({get: () => {return 2021 - data.age},set: val => {data.age = 2021 - val}})})watch(() => props.title, (newTitle, oldTitle) => {console.log(newTitle, oldTitle)context.emit('title-changed')})function changeAge(val) {data.age += valconsole.log(props.title)context.emit('title-changed')}function changeYear(val) {data.year = data.year + val}return {...toRefs(data), changeAge, changeYear}}
}
vue3 学习之路1相关推荐
- Redis学习之路(一)--下载安装redis
redis学习之路--下载安装redis windows安装redis 1.下载redis 2.安装 3.查看是否安装成功 windows安装redis 1.下载redis 网址:https://gi ...
- 前端Vue学习之路(二)-Vue-router路由
Vue学习之路 (二) Vue-router(基础版) 一.增加静态路由 二.动态路由+路由嵌套+404页面 三. 编程式导航 四.命名路由 五.命名视图 六.重定向和起别名 1.重定向 2.起别名 ...
- 前端Vue学习之路(一)-初识Vue
Vue学习之路 (一) 1.引言 2.更换npm国内镜像源 3.用npm下载Vue 4.Vue全家桶 5.使用命令创建项目 5.推荐插件 6.推荐网站 7.学习扩展 1.引言 先安装node.js环境 ...
- 学习之路-现代密码学基础-001
学习之路-现代密码学基础-第一章密码学概论 转载于:https://www.cnblogs.com/vegetables-Adanos/p/5371420.html
- 拿下斯坦福和剑桥双offer,00后的算法学习之路
董文馨,00后,精通英语,西班牙语.斯坦福大学计算机系和剑桥大学双Offer,秋季将进入斯坦福大学学习. 10岁开始在国外上学:12岁学Scratch: 13岁学HTML & CSS: 14岁 ...
- Markdown学习之路
Markdown学习之路 作者:CFishHome 转载请注明地址:https://blog.51cto.com/12731497/2164274 Markdown是什么? Markdown是一个 W ...
- [EntLib]微软企业库5.0 学习之路——第五步、介绍EntLib.Validation模块信息、验证器的实现层级及内置的各种验证器的使用方法——上篇...
本文是为后面的学习之路做铺垫,简单介绍下企业库中的Validation模块的一些相关知识,包括Validation模块的简介.用途.使用方法.默认提供的多种验证器的介绍等. 一.简介及用途 在实际的项 ...
- 转载: Qt 学习之路 2归档
Qt 学习之路 2归档 http://www.devbean.net/2012/08/qt-study-road-2-catelog/
- python之路 mysql 博客园_教为学:Python学习之路(二):MySQLdb的几种安装方式,以及用Python测试连接MySql...
教为学:Python学习之路(二):MySQLdb的几种安装方式,以及用Python测试连接MySql Easy_install安装MySQLdb 很简单,以至于我不晓得该怎么说.一句话. sodu ...
最新文章
- 蚂蚁金服-支付风险识别亚军方案!
- Tomcat 6.0.32 +Spring dbcp datasource关闭Tomcat出现严重异常
- linux编译mysql报无法将左值_'错误:无法将'std::ostream {aka std::basic_ostream
- lol什么服务器出无限活力,《LOL》无限火力模式什么时候出 无限火力模式上线时间一览...
- AI:IPPR的数学表示-CNN结构进化(Alex、ZF、Inception、Res、InceptionRes)
- log4net 在asp.net WEB应用程序中的配置
- 【LeetCode从零单排】No118	Pascal#39;s Triangle
- flutter android 和 ios 发布
- wiquery ResizePanel
- SNPP/VIIRS 数据介绍和下载
- CSS——简写属性(在padding和margin这样的简写属性中,值赋值的顺序是top、right、bottom、left)...
- 机器视觉软件工程师的生活是怎样的?
- kdj指标主要看哪个值_史上最全KDJ指标用法详解,学习KDJ指标看这一篇就够了
- 浅谈在线IDE的搭建,配置,体验
- Java private方法访问
- NCE4 L6 The sporting spirit
- 介绍java 8 的 Period 和 Duration 类
- array_unshift php,php array_unshift函数怎么用?
- iOS 视频播放(AVPlayer)
- 今天安利几个好用的软件给你
热门文章
- 二叉排序树遍历二叉树打印简单图书管理系统
- Kotlin学习历程——对象声明与伴生对象
- 彭蕾:阿里需要什么样的人?| 内部干货
- Non-exhaustive, Overlapping Clustering《非详尽的、可重叠的聚类》论文算法的解读(学习笔记)
- sql如何查询表的第一条记录和最后一条记录
- suricata-update管理规则
- 整流器,二极管损耗,电容补偿介绍
- 冯.诺依曼计算机硬件部分是由五大功能部件组成,计算机硬件由哪五大功能部件组成,每一个部件的作用是什么?...
- html 判断是否顶层,为什么很多人都不考虑买顶楼! 看了这些您就知道了!
- HDU-3717:Rescue(二分枚举+模拟)