目录

uniappscript-setup-langts使用-uni.emit和uni.on全局传递数据

目录

uniapp+<script setup lang=“ts“>使用 uni.$emit和uni.$on全局传递数据

注意:

在A页面直接使用 uni.$emit(‘changeCategoryKey’, childCategory)传递,在B页面使用  uni.$on(‘changeCategoryKey’, (val) => {console.log(val, ‘取值’);});只在组件传递有效,页面跳转后是无效的

跳转页面使用的传递数据的方法如下:

A页面传递传递:

const goVisitList = (childCategory, type) => {
    uni.$on('sendCategoryKey', () => {
        uni.$emit('changeCategoryKey', childCategory);
    });
    uni.navigateTo({
        url: `/sub-pages/goods/visit-list/index?categoryType=${categoryKey}`,
    });
};

接收:

onLoad(() => {
    uni.$on('changeCategoryKey', (val) => {
        console.log(val, '取值');
    });
    uni.$emit('sendCategoryKey');
});

https://i-blog.csdnimg.cn/direct/13c2507f2964409198e0261331c99a68.png