免費(fèi)做網(wǎng)站安全嗎黑鋒網(wǎng)seo
問題描述
uniapp常用的有:頁面和組件,并且頁面和組件各自有各自的生命周期函數(shù),那么在頁面/組件請求數(shù)據(jù)時,是用created呢,還是用onLoad呢?
先說結(jié)論:
組件使用組件的生命周期,頁面使用頁面的生命周期。
例如:組件使用created請求數(shù)據(jù),頁面使用onLoad請求數(shù)據(jù)。
參考文章:
uni-app子組件onLoad、onReady事件無效_uniapp 組件 onload-CSDN博客
可以看到,Header組件并沒有觸發(fā)onLoad和onReady函數(shù),所以組件中不要使用頁面的生命周期。
<template><view class="content"><Header /></view>
</template><script>import Header from '../../components/Header/Header.vue'export default {components: {Header},data() {return {title: 'Hello'}},mounted() {console.log('index mounted');},created() {console.log('index created');},onReady() {console.log('index onReady');},onInit() {console.log('index onInit');},onLoad() {console.log('index onLoad');},onShow() {console.log('index onShow');},methods: {}}
</script>
<template><view>this is header</view>
</template><script>export default {name:"Header",data() {return {};},mounted() {console.log('header mounted');},created() {console.log('header created');},onReady() {console.log('header onReady');},onInit() {console.log('header init');},onLoad() {console.log('header load');},onShow() {console.log('header show');}}
</script><style lang="scss"></style>