官方叙述的拦截器配置: 官方叙述的拦截器配置https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/ts-basic-components-navigation-V13#%E7%A4%BA%E4%BE%8B2%E4%BD%BF%E7%94%A8%E8%B7%AF%E7%94%B1%E6%A0%88%E6%96%B9%E6%B3%95
src/main/ets
├── pages
│ └── navigation
│ ├── views
│ │ ├── Mine.ets // 个人中心页
│ │ ├── Login.ets // 登录页
│ │ └── ErrorPage.ets // 错误页
│ └── NavagationIndex.ets // 导航入口
├── routerMap.json5 // 路由配置文件
{
"routerMap": [
{
"name": "mine", // 路由唯一标识符
"buildFunction": "MineBuilder",
"pageSourceFile": "src/main/.../Mine.ets"
},
// ...其他路由项
]
}
关键字段含义
关键字段 | 说明 |
---|---|
| 路由标识符,用于编程式导航 |
| 页面构建器函数名(关联组件注册) |
| 源代码文件位置(动态加载基础) |
@Entry
@Component
struct TextPage {
@Provide
navPathStack:NavPathStack = new NavPathStack()
build() {
Navigation(this.navPathStack){
Text('这是首页')
Button('跳到下一页去')
.onClick(()=>{
this.navPathStack.pushPath({name:'nextOne'})
})
}
}
}
@Builder
function PageOne() {
TextPage_()
}
@Component
struct TextPage_ {
@Consume
navPathStack: NavPathStack
build() {
NavDestination() {
Text('这是我从首页面跳过来的第一个页面')
Image($r('app.media.Cover'))
.width(100)
.aspectRatio(1)
Button('跳到第三个页面去')
.onClick(() => {
this.navPathStack.pushPath({ name: 'pageTwo' })
})
}
}
}
@Builder
function PageTwo() {
TextPage2_()
}
@Component
struct TextPage2_ {
build() {
NavDestination() {
Text('这是我从首页面跳过来的第二个页面')
Image($r('app.media.Cover2'))
.width(100)
.aspectRatio(1)
}
}
}
```txt
{
"name": "nextOne",
"buildFunction": "PageOne",
"pageSourceFile": "src/main/ets/pages/TextPage_.ets"
},
{
"name": "pageTwo",
"buildFunction": "PageTwo",
"pageSourceFile": "src/main/ets/pages/TextPage_.ets"
}
@Entry
@Component
struct NavagationIndex {
@Provide navPathStack: NavPathStack = new NavPathStack()
build() {
Navigation(this.navPathStack) {
// 初始页面内容
Button('跳转至-我的').onClick(() => {
this.navPathStack.pushPath({name: 'mine'})
})
}
.mode(NavigationMode.Auto) // 跨设备适配
}
}
模式 | 适用场景 |
---|---|
NavigationMode.Stack | 单页面栈模式(移动端) |
NavigationMode.Split | 分栏模式(平板/PC) |
NavigationMode.Auto | 自动适配设备 |
.onAppear(() => {
this.registerInterceptors()
})
const token = AppStorage.get('token')! as string || '';
if (t.pathInfo?.name === 'mine' && token === '') {
// 拦截动作分解
t.pathStack.pop(); // 移除无效跳转
t.pathStack.pushPath({ // 重定向到登录页
name: "login"
});
}
if (!t.pathInfo && f.pathInfo.name !== 'error') {
f.pathStack.pushPath({name: 'error'}) // 注入错误页
return;
}
import { promptAction } from '@kit.ArkUI'
@Entry
@Component
struct NavagationIndex {
@Provide
navPathStack: NavPathStack = new NavPathStack()
registerInterceptors() {
this.navPathStack.setInterception({
//f从哪来,t到哪去
willShow: (f, t) => {
if (typeof t === 'string'||typeof f ==='string') {
return
}
//f可能是首页跳过来的
//必须加个逻辑与
// if(!t.pathInfo && f.pathInfo.name !='error'){
// f.pathStack.pushPath({name:'error'})
// return
// //返回不好返回
// }
if (t.pathInfo.name === 'mine') {
promptAction.showToast({
message: '拦截到我要去mine'
})
//拦截你 不让你跳
// t.pathStack.pop()
const token = AppStorage.get('token')! as string || ''
//拦截!!!
if (token === '') {
t.pathStack.pop()
//去登录
t.pathStack.pushPath({
name: "login"
})
}
return
}
},
//t 跳转之后的拦截
didShow: () => {
}
})
}
build() {
Navigation(this.navPathStack) {
//还没有进行跳转,但是已经有展示的内容了
Text('这是我的首页')
Button('跳转至-我的')
.onClick(() => {
this.navPathStack.pushPath({
name: 'mine'
})
})
}
.onAppear(() => {
this.registerInterceptors()
})
.height('100%')
.width('100%')
//跨设备
.mode(NavigationMode.Auto)
}
```ts
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。