<script>
import { ref, reactive, readonly, toRefs, computed, watch ,provide,inject} from "vue";
export default {
setup(){
let name= ref(‘tom‘);
let user = reactive({
name:‘‘tom,
age:10
});
provide("name",name);//分享自己的name属性
provide("user",user );//分享自己的user对象
return{
name,
...toRefs(user)
}
}
};
</script>
子组件:
<script>
import { ref, reactive, readonly, toRefs, computed, watch ,provide,inject} from "vue";
export default {
setup(){
let name = inject(‘name‘);//注入name属性
let user = inject(‘user‘);//注入user对象
return{
name,
user
}
}
};
</script>