import {ref, watch} from "vue";
export default {setup() {const p1 = ref(0);const p2 = ref(1);// 监听 p1 的数据变化, newVal 是最新的数据 watch(p1,(newVal, oldVal)=>{console.log(newVal, oldVal)});watch(p2,(newVal, oldVal)=>{console.log(newVal, oldVal)},{immediate: true});//进入页面立即开始监听return {p1,p2}}
}
监听多个,批量监听
import {ref, watch, reactive} from "vue";
export default {setup() {const p1 = ref(0);const p2 = ref(1);const p3 = reactive({name: "海绵宝宝",age: {num: 1}})//批量监听数据变化, 监听 p1 的数据变化, newVal 是最新的数据 watch([p1,p2],(newVal, oldVal)=>{console.log(newVal, oldVal)//会打印出来数组的结果[]});//监听整个 reactive响应式数据的变化:只能监听到最新的结果,无法获取到上一次的数据watch(p3,(newVal, oldVal)=>{console.log(newVal, oldVal)//会打印Proxy 对象。});//监听 reactive响应式数据中某一个属性值的变化:最新的结果和上一次结果都可以拿到watch(() => p3.age.num,(newVal, oldVal)=>{console.log(newVal, oldVal)//会打印Proxy 对象。});return {p1,p2,p3}}
}
import {ref, watch, reactive} from "vue";
export default {setup() {const p1 = ref(0);const res = watchEffect(()=>{let a = p1.value;console.log(a)//只要 p1.value 有变化就会被监听到,且进入页面就会调用打印一次});res();//停止监听return {res ,p1,}}
}
import { shallowRef, shallowReactive} from "vue";
export default {setup() {//只能处理第一层数据const p1 = shallowReactive({name: "蜡笔小新",// 第一层数据age: {num: 1//第二层数据}});// shallowRef 只能处理基本数据类型,如果是复杂类型,则视图不展示return {...toRefs(p1),}}
}
vue3 中父组件的样式不需要添加 scoped
//父(祖)组件
import {provide, reactive} from "vue";
export default{name: "App",components: {ChildDom},setup() {const p1 = reactive({name: "我是祖组件",age: 1000});provide("p1Obj", p1);//传值return {p1}}
}
//子(孙)组件 ChildDom.vue
import {inject} from "vue";
export default{name: "App",components: {ChildDom},setup() {const p1 = inject("p1Obj");//接收值console.log(p1 )//打印出的结果就是 响应式的数据return {p1}}
}
//父(祖)组件
import {ref, reactive} from "vue";
export default{name: "App",components: {ChildDom},setup() {const p1 = reactive({name: "我是祖组件",age: 1000});provide("p1Obj", p1);//传值let myChildDom = ref();function btnHandle() {myChildDom.value.recive(p1)//recive 是子组件的方法}return {p1,myChildDom}}
}
//子(孙)组件 ChildDom.vue
export default{name: "App",components: {ChildDom},setup() {function recive(p1) {console.log("我是被父组件调用的方法",p1 )//打印出的结果就是 响应式的数据}return {recive}}
}
store/index.js
import {createStore} from "vuex";
createStore({//创建数据仓库state: {name: "海绵宝宝"},//同步调用mutations: {trigger(state) {state.name = "派大星";console.log("我是被action 调用的");},trigger2(state, name) {state.name = name;console.log("我是被组件调用的");}},//异步调用actions: {sub(store) {console.log("*****");store.commit("trigger");}}})main.js
import createStore from './store/index,js';
createApp(App).use(createStore);//组件
import {useStore} from "vuex";
import {computed} from "vue";
export default{setup() {// 从 vuex 数据仓库里取数据const store = useStore();const res = computed(()=>{console.log(store.state.name);return store.state.name})// 修改数据仓库的内容function btn() {//异步调用 dispatchstore,dispatch("sub");// 同步调用store.commit("trigger2", "派大星")}return {res,btn}}
}
创建―在组件创建时执行。
挂载—DOM被挂载时执行。
更新—当响应数据被修改时执行。
销毁一在元素被销毁之前立即运行
·使用需引入
import {ref, reactive} from "vue";
export default{setup() {onBeforeMount(() => {})}
}
// 公用的数据或者方法
import {reative, computed, watch} from "vue";
const common = () => {const res = reative({name: "龙王",age: 5000,company: "水晶宫" })return {res}
}
export default common;// 组件中使用
//首先引入
import common from "common.js"
export default {setup(){const res = common();//则直接拿到 res 的响应式数据return {res};//通过此步骤就可以进行 页面的渲染。}
}