1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <template> <div> {{computedName}} </div> </template> <script> import { reactive,ref,computed,watch,watchEffect } from 'vue'; export default { setup(){ const obj = reactive({ myname:"aaa", myage:11, datalist:[] }) const mystr = ref("bbb")
const computedName = computed(()=>obj.myname.toUpperCase()) watch(mystr,async (value)=>{ var res = await axios.get(`http://localhost:3000/news?author=${value}`) obj.datalist = res.data },{immediate:true}) watch(()=>obj.myname,(newValue,oldValue)=>{ console.log("同步/异步",newValue,oldValue) }) watch([obj,mystr],(newValue,oldValue)=>{ console.log("同步/异步",newValue,oldValue) })
watchEffect(async ()=>{ const res = await axios.get(`http://localhost:3000/news?author=${mystr.value}`) obj.datalist = res.data }) return { obj, mystr, computedName, } } } </script>
|