uniapp中使用vue3, 如果要使用页面生命周期函数,如: onLoad 与 onShow 事件 , 需要从
@dcloudio/uni-app 包中解构出来. 代码如下:
<script setup>
import {
onLoad,
onShow
} from "@dcloudio/uni-app";
// 计算属性与监听属性是在vue中而非uniap中 需要注意!!!
import {
watch,
computed
}
from "vue";
onLoad((options) => {
console.log('页面初始化')
});
onShow(() => {
console.log('页面出现时')
});
const name = ref('默认值');
const name2 = ref ('默认值2')
const name = computed(() => {
let style = ""
// do something
// 当这个方法内的所有用到的值发生改变时 都会重新执行并计算
return style ;
})
const name2 = computed(() => {
let style = ""
// do something
return style ;
})
// 假设监听 一个 name 变量
watch(name, (newValue, oldValue) => {
// do something
// 这里就是当 name 属性值发生变化时 ,就会执行此方法
})
// 如果需要监听多个变量,且要做的事情是一样的 那么我们只需要把第一个参数变为数组即可
watch([name, name2], (newValue, oldValue) => {
// do something
// 这里就是当 name或者name2 值发生变化时 ,就会执行此方法
})
</script>