11.导航跳转与传参

普通导航

示例代码:

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
<template>

<navigator open-type="navigate"
url="/pages/detail/detail">跳转至详情页</navigator>

<navigator open-type="switchTab"
url="/pages/message/message">跳转至信息页</navigator>

<navigator open-type="redirect"
url="/pages/detail/detail">跳转至详情页</navigator>

</template>

<script>

export default {

onUnLoad() {

console.log('页面卸载了'); //
switchTab跟redirect都无法返回上一页面,所以会把导航页给卸载掉

}

}

</script>

其中switchTab必须指向带tabbar的页面,否则无法生效

而redirect则会替换掉原有的页面的历史记录,导致用户无法通过点击左上角的返回按钮返回上一页面。

注意:跟pages.json的路径配置规则不同,导航路径的前面必须加上斜杠

编程式导航

示例代码:

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
47
48
49
50
51
52
<template>

<button type="primary" @click="goDetail">跳转至详情页</button>

<button type="primary" @click="goMessage">跳转至信息页</button>

<button type="primary"
@click="redirectDetail">跳转至详情页</button>

</template>

<script>

export default {

methods: {

goDetail() {

uni.navigateTo({

url: "/pages/detail/detail"

})

},

goMessage() {

uni.switchTab({

url: "/pages/message/message"

})

},

redirectDetail() {

uni.redirectTo({

url: "/pages/detail/detail"

})

}

}

}

</script>

传参

示例代码:

在navigator.vue中:

1
2
3
4
5
6
<template>

<navigator open-type="navigate"
url="/pages/detail/detail?id=80&name=Mike">跳转至详情页</navigator>

</template>

在detail.vue中:

1
2
3
4
5
6
7
8
9
export default {

onLoad(options) {

console.log(options);

}

}

其中onLoad的options就是专门用来接收传入参数的。

参考:

24-两种导航跳转和传参


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!