在 uni.app 项目中,跳转页面传入过多参数,页面进行刷新时,会打不开

... 2022-11-17 Less than 1 minute

# 在 uni.app 项目中,跳转页面传入过多参数,页面进行刷新时,会打不开

例如


<template>
  <view @click="handleNavTo">跳转</view>
</template>

<script>
export default {
    data () {
        return {}
    },
    methods: {
        handlNavTo() {
            const urlData = [{ a: 2, b: 3, c: 5}]
            uni.navigateTo({
                url: `/pages/user/index?data={JSON.stringify(urlData)}`
            })
        }
    }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

解决方案

使用 eventChannel 来传参数,如需要持久化需加到本地缓存中。


// 父页面
<template>
  <view @click="handleNavTo">跳转</view>
</template>

<script>
export default {
    data () {
        return {}
    },
    methods: {
        handlNavTo() {
            const urlData = [{ a: 2, b: 3, c: 5}]
            uni.navigateTo({
                url: `/pages/user/index`,
                success: (res) => {
                    // 触发事件
                    res.eventChannel.emit('toUrl', {data: urlData})
                }
            })
        }
    }
}
</script>

// 子页面
<template>
  <view @click="handleNavTo">跳转</view>
</template>

<script>
export default {
    data () {
        return {}
    },
    onload() {
        // 获取事件总线
        const eventChannel = this.getOpenerEventChannel()
        // 监听事件
        eventChannel.on('toUrl', function (res) {
            
        })
    }
}
</script>
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
Last update: November 17, 2022 07:05
Contributors: Skr