Navigation 设置路由拦截直接异常,无法执行
interface item {
url?:string,
label:string
}
@Entry
@Component
struct NavigationExample {
@State TooTmp: ToolbarItem = {'value': "func", 'icon': "./image/ic_public_highlights.svg", 'action': ()=> {}}
private arr: number[] = [1, 2, 3];
pathStack: NavPathStack = new NavPathStack()
@Builder ToolBar() {
Row() {
this.toolBarItem({label:'fun1'})
this.toolBarItem({label:'fun2'})
this.toolBarItem({label:'fun3'})
}.borderRadius(20).backgroundColor('red')
}
@Builder toolBarItem($$:item) {
Column(){
Image($r('app.media.vip_manage_view_unbing'))
Text($$.label)
}
}
aboutToAppear() {
this.pathStack.setInterception({
willShow: (from: NavDestinationContext | "navBar", to: NavDestinationContext | "navBar",
operation: NavigationOperation, animated: boolean) => {
if (typeof to === "string") {
console.log("target page is navigation home page.");
return;
}
// 将跳转到PageTwo的路由重定向到PageOne
let target: NavDestinationContext = to as NavDestinationContext;
if (target.pathInfo.name === 'PageTwo') {
target.pathStack.pop();
target.pathStack.pushPathByName('PageOne', null);
}
}
})
}
jump(){
this.pathStack.pushPathByName('PageOne', null)
}
build() {
Column() {
Navigation(this.pathStack) {
List({ space: 12 }) {
ListItem() {
Button("跳转1").onClick((event: ClickEvent) =>{
this.jump()
})
}
ListItem() {
Button("跳转2").onClick((event: ClickEvent) => {
})
}
}
.width("90%")
.margin({ top: 12 })
}
.title("主标题")
.mode(NavigationMode.Stack)
.hideTitleBar(true)
.toolbarConfiguration(this.ToolBar)
}
.height('100%')
.width('100%')
.backgroundColor('#F1F3F5')
}
}
navigation可以封装公共路由栈方便路由管理和拦截。
具体操作
3、在其他页面entry/hsp/har中引入公共类import { CommonRouter } from "@ace/library";然后调用CommonRouter.getInstance().pushPathByName('pageTwo'),CommonRouter.getInstance().clear(),CommonRouter.getInstance().pop()即可.