Vue3 ref函数 数据响应式
1、作用:定义响应式数据
2、语法
a、引用
import { ref } from 'vue';
b、创建
创建一个包含响应式数据的引用对象
let xx= ref(数据)
c、JS操作
xx.value
d、模板操作
{{xx}}
3、注意
数据可以是:基本类型,也可以是对象类型
基本类型需要.value获取值,对象类型不需要
案例
<template> <h2>姓名:{{name}}</h2> <h2>年龄:{{age}}</h2> <h2>工作:{{job.wMode}}</h2> <h2>薪水:{{job.salary}}</h2> <button @click="changeInfo">点击</button> </template> <script> import { ref } from 'vue'; export default { name: 'App', components: {}, setup() { let name = ref('jojo') let age = ref(8) let job = ref({ wMode: 996, salary: 2800 }) function changeInfo(){ name.value = 'duke' age.value = 4 job.value.wMode = '855' job.value.salary = 3500 // console.log(name, age) } return { name, age, changeInfo, job } } } </script> <style> </style>