ref函数实现响应式数据
文章目录ref函数处理基本类型ref函数处理对象类型ref函数处理基本类型入口文件main.jsimport { createApp } from 'vue';import App from './App.vue';createApp(App).mount('#app')App.vue<template><h1>个人信息</h1><h2>姓名:{{n
·
ref函数处理基本类型
- 入口文件main.js
import { createApp } from 'vue';
import App from './App.vue';
createApp(App).mount('#app')
- App.vue
<template>
<h1>个人信息</h1>
<h2>姓名:{{name}}</h2>
<h2>年龄:{{age}}</h2>
<button @click="changeInfo">修改个人信息</button>
</template>
<script>
import {ref} from "vue";
export default {
name: 'App',
setup(){
let name = ref("张三");
let age = ref(18);
function changeInfo(){
name.value = "李四";
age.value = 28;
}
return {
name,
age,
changeInfo
}
}
}
</script>
- 启动应用,测试效果
ref函数处理对象类型
- 入口文件main.js
import { createApp } from 'vue';
import App from './App.vue';
createApp(App).mount('#app')
- App.vue
<template>
<h1>个人信息</h1>
<h2>姓名:{{name}}</h2>
<h2>年龄:{{age}}</h2>
<h2>岗位:{{job.position}}</h2>
<h2>薪酬:{{job.salary}}</h2>
<button @click="changeInfo">修改个人信息</button>
</template>
<script>
import {ref} from "vue";
export default {
name: 'App',
setup(){
let name = ref("张三");
let age = ref(18);
let job = ref({
position:"前端工程师",
salary:"30k"
});
function changeInfo(){
name.value = "李四";
age.value = 28;
job.value.position = "Java工程师";
job.value.salary = "60k";
}
return {
name,
age,
job,
changeInfo
}
}
}
</script>
- 启动应用,测试效果
更多推荐
已为社区贡献19条内容
所有评论(0)