setup(props, context) {
//处理基本类型
const {
ref } = Vue;
//现在name就是响应式的数据,2秒后改变,依然会更新视图
let name = ref('123');
setTimeout(() => {
name.value = '456'
}, 2000)
return {
name}
}
setup(props, context) {
//处理对象,数组类型
const {
reactive } = Vue;
//现在people就是响应式的数据,2秒后改变,依然会更新视图
const people = reactive({
name: '张三', gender: '男'});
setTimeout(() => {
people.name = '李四'
}, 2000)
return {
people}
}
setup(props, context) {
//处理对象,数组类型解构之后
const {
reactive, toRefs } = Vue;
//现在people就是响应式的数据,2秒后改变,依然会更新视图
const people = reactive({
name: '张三', gender: '男'});
setTimeout(() => {
people.name = '李四'
}, 2000)
//把people里面的单个属性变为响应式
const {
name, gender } = toRefs(people);
return {
name, gender }
}
setup(props, context) {
//处理对象,数组类型解构之后
const {
reactive, toRef } = Vue;
//现在people就是响应式的数据,2秒后改变,依然会更新视图
const people = reactive({
name: '张三', gender: '男'});
setTimeout(() => {
people.name = '李四'
}, 2000)
//把people里面的单个属性变为响应式
//注意age一开始是没有哒
const {
name, gender, age} = toRef(people,'age');
return {
name, gender,age }
}
setup(props, context) {
//处理对象,数组类型解构之后
const {
reactive, readonly } = Vue;
//现在people就是响应式的数据,2秒后改变,依然会更新视图
const people = readonly({
name: '张三', gender: '男'});
setTimeout(() => {
//会报错
people.name = '李四'
}, 2000)
return {
people }
}
setup(props, context) {
//attrs第一个参数是None-Props属性
//slot是一个方法,调用default方法会返回一个VNODE
//emit是各种事件
const {
attrs, slots, emit } = context;
//获取属性
//console.log(attrs.属性名)
//渲染函数
//const { h } = Vue;
//渲染父组件传递过来的插槽
//return ()=>h('div',{},slots.default())
//触发消息事件
//function handleClick() { emit('change'); }
//return { handleClick }
}
const app = Vue.createApp({
setup() {
//要引用进来这个computed
const {
reactive, computed } = Vue;
const countObj = reactive({
count: 0});
const handleClick = () => {
countObj.count += 1;
}
//和以前一样,使用get与set
let countAddFive = computed({
get: () => {
return countObj.count + 5;
},
set: (param) => {
countObj.count = param - 5;
}
})
setTimeout(() => {
countAddFive.value = 100;
}, 3000)
return {
countObj, countAddFive, handleClick }
},
setup() {
const {
ref, watch } = Vue;
//基本类型
const age = ref(18)
//监听age
watch(age,(oldValue,newValue)=>{
console.log("oldValue:",oldValue,"newValue:",newValue,)
})
return {
age}
}
setup() {
const {
reactive, watch,toRefs} = Vue;
//对象
const people = reactive({
age:18})
//监听age
watch(()=>people.age,(oldValue,newValue)=>{
console.log("oldValue:",oldValue,"newValue:",newValue,)
})
//结构一下,直接把这个age转化为响应式,可以直接使用
const {
age} = toRefs(people);
return {
age}
}
setup() {
const {
ref,watchEffect,toRefs,reactive} = Vue;
//基本类型
const age = ref(18)
//对象
const people = reactive({
name:'张三'})
//监听
watchEffect(() => {
console.log(age);
console.log(people.name);
})
return {
age}
}
过渡
新增
父组件setup函数中
setup() {
const {
provide} = Vue;
//给后代传递age变量,值为18
provide('age', 18);
return {
}
}
后代组件setup函数中
setup() {
const {
inject } = Vue;
//接收age变量,默认值为20
const name = inject('age', 20);
return {
}
}
<div>
<div ref="a">adiv>
div>
setup() {
const {
ref, onMounted } = Vue;
//这里的a一定是和标签中的ref值对应,然后给ref传null
const a = ref(null);
onMounted(() => {
//注意这里是ref转化的响应式数据,a.value才是dom哈
console.log(a.value);
})
return {
a }
}