提交 ad182749 编写于 作者: Q qiang

feat: 添加拦截器示例

上级 ed8efb5e
...@@ -367,6 +367,24 @@ ...@@ -367,6 +367,24 @@
"style": { "style": {
"navigationBarTitleText": "获取定位" "navigationBarTitleText": "获取定位"
} }
},
{
"path": "pages/API/interceptor/interceptor",
"style": {
"navigationBarTitleText": "拦截器"
}
},
{
"path": "pages/API/interceptor/page1",
"style": {
"navigationBarTitleText": "拦截器测试页面 1"
}
},
{
"path": "pages/API/interceptor/page2",
"style": {
"navigationBarTitleText": "拦截器测试页面 2"
}
}, },
{ {
"path": "pages/tabBar/CSS", "path": "pages/tabBar/CSS",
......
<template>
<view>
<button @click="addInterceptor">添加拦截器</button>
<button @click="removeInterceptor">移除拦截器</button>
<button @click="navigateTo">跳转到测试页面</button>
</view>
</template>
<script>
const interceptor = {
invoke: function (options : NavigateToOptions) {
console.log('interceptor.invoke:', options)
const url = './page2'
uni.showToast({
title: `重定向到页面:${url}`
})
options.url = url
},
success: function (res : NavigateBackSuccess) {
console.log('interceptor.success:', res)
},
fail: function (err : NavigateToFail) {
console.log('interceptor.fail:', err)
}
} as Interceptor
export default {
data() {
return {
}
},
methods: {
addInterceptor() {
uni.addInterceptor('navigateTo', interceptor)
uni.showToast({
title: '拦截器已添加'
})
},
removeInterceptor() {
uni.removeInterceptor('navigateTo', interceptor)
uni.showToast({
title: '拦截器已移除'
})
},
navigateTo() {
uni.navigateTo({
url: './page1',
success(res) {
console.log('res:', res)
},
fail(err) {
console.error('err:', err)
}
})
}
}
}
</script>
<style>
</style>
\ No newline at end of file
<template>
<view>
<text style="text-align: center;">测试页面 1</text>
</view>
</template>
<script>
export default {
data() {
return {
}
},
methods: {
}
}
</script>
<style>
</style>
\ No newline at end of file
<template>
<view>
<text style="text-align: center;">测试页面 2</text>
</view>
</template>
<script>
export default {
data() {
return {
}
},
methods: {
}
}
</script>
<style>
</style>
\ No newline at end of file
...@@ -77,6 +77,11 @@ ...@@ -77,6 +77,11 @@
name: '事件总线event-bus', name: '事件总线event-bus',
url: 'event-bus', url: 'event-bus',
api: ["$on", "$once", "$off", "$emit"] api: ["$on", "$once", "$off", "$emit"]
},
{
name: '拦截器',
url: 'interceptor',
api: ["addInterceptor", "removeInterceptor"]
}, },
{ {
name: '获取启动参数', name: '获取启动参数',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册