模板项目 index.vue 改动
<template>
<view class="content">
<image class="logo" src="../../static/logo.png" @click="onClick"></image>
<view>
<text class="title">{{title}}</text>
<test v-if="showTest" />
</view>
</view>
</template>
<script lang="ts">
import Vue from 'vue';
import Test from './test.vue';
export default Vue.extend({
data() {
return {
title: 'Hello',
showTest: true,
}
},
components: {
Test,
},
onLoad() {
},
methods: {
onClick() {
this.showTest = !this.showTest;
},
}
});
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin: 200rpx auto 50rpx auto;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>
test.vue 代码
<template>
<text class="test">hahaha</text>
</template>
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
data() {
return {
observer: null,
} as {
observer: UniApp.IntersectionObserver | null;
};
},
created() {
this.createObserver(() => {}, 'test');
},
destroyed() {
this.disconnectObserver();
},
methods: {
createObserver(cb: { (): Promise<void> | void; (): void; }, calssName: string) {
this.$nextTick(() => {
this.disconnectObserver();
this.observer = uni.createIntersectionObserver(this).relativeToViewport({ bottom: -100 }); // 底部有导航栏覆盖,这里从导航栏往上设置可视区域
this.observer.observe(calssName, (res) => {
if (res.intersectionRatio === 0) {
return;
}
cb();
this.disconnectObserver(); // 出现后关闭监听
});
});
},
disconnectObserver() {
if (this.observer) {
console.log('----- disconnectObserver -----');
this.observer.disconnect();
this.observer = null;
}
}
}
});
</script>
l***@gmail.com (作者)
我还怀疑过是不是小程序底层的问题,从我新上传的 demo 来看,小程序底层看起来没有内存泄漏的问题,辛苦大佬排查一下看看。
2024-07-18 16:23