参数类不支持传递如map等JS对象。
interface paramType {
param: string
}
let paramA: paramType = {
param: 'test1'
}
@Entry
@Component
struct Index {
@Provide('pathInfos') pathInfos: NavPathStack = new NavPathStack();
@Builder
myRouter(name: string) {
if (name === 'MyFirstNavDestination') {
MyFirstNavDestination()
} else if (name === 'MySecondNavDestination') {
MySecondNavDestination()
}
}
build() {
Navigation(this.pathInfos) {
Row() {
Column() {
Text('hello world')
}
.height('100%')
}
.onClick(() => {
this.pathInfos.pushPathByName('MyFirstNavDestination', paramA);
})
}
.navDestination(this.myRouter)
}
}
@Component
export struct MyFirstNavDestination {
@Consume('pathInfos') pathInfos: NavPathStack;
getParamsPrint() {
console.info('xuerui', 'param is ' + JSON.stringify(this.pathInfos.getParamByName('MyFirstNavDestination')));
}
build() {
NavDestination() {
Row() {
Column() {
Text('MyFirstNavDestination')
}
.width('100%')
}
.height('100%')
.onClick(() => {
this.pathInfos.pushPathByName('MySecondNavDestination', null);
})
}.onShown(() => {
this.getParamsPrint();
})
}
}
@Component
export struct MySecondNavDestination {
@Consume('pathInfos') pathInfos: NavPathStack;
private routerParams: paramType = { param: 'test 2' };
build() {
NavDestination() {
Row() {
Text('MySecondNavDestination')
}
.height('100%')
}.onBackPressed(() => {
// Pop B page
this.pathInfos.pop();
//Get the name of the current stack top page (page A)
let allPathName: Array<string> = this.pathInfos.getAllPathName();
let pathNameA: string = allPathName[allPathName.length - 1];
// Pop A page
this.pathInfos.pop();
// PUSH A page again
this.pathInfos.pushPath(new NavPathInfo(pathNameA, this.routerParams))
return true;
})
}
}