HarmonyOS鸿蒙Next中router.pushurl里不同模块之间的跳转
HarmonyOS鸿蒙Next中router.pushurl里不同模块之间的跳转 从youxi模块的loginPage push到common模块的webpage页面,这个跳转地址(url)要怎么写
router已经不再演进,推荐使用Navigation路由,可参考以下代码:
在MainHar中
import { harAPage } from 'harA/src/main/ets/components/mainpage/harAPage';
@Entry
@Component
struct Index {
@State message: string = 'Navigation能力提供';
@Provide('pageStack') pageStack: NavPathStack = new NavPathStack();
@Builder
pageMap(name: string) {
if (name === 'harAPage') {
harAPage();
}
}
build() {
Navigation(this.pageStack) {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button("去HarA").onClick(() => {
let pathInfo: NavPathInfo = new NavPathInfo('harAPage', "111111", (popInfo: PopInfo) => {
})
this.pageStack.pushDestination(pathInfo,true);
})
}
.width('100%')
}.navDestination(this.pageMap)
}
}
在HarA中:
@Component
export struct harAPage {
@State message: string = 'HarA';
@Consume('pageStack') pageStack: NavPathStack;
build() {
NavDestination(){
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
.width('100%')
}
.height('100%')
}
}
}
更多关于HarmonyOS鸿蒙Next中router.pushurl里不同模块之间的跳转的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,router.pushUrl 用于页面之间的跳转。不同模块之间的跳转可以通过指定目标页面的路径来实现。路径格式通常为"模块名/页面名"。
例如,假设有两个模块ModuleA和ModuleB,ModuleA中有一个页面PageA,ModuleB中有一个页面PageB。如果要从PageA跳转到PageB,可以使用以下代码:
router.pushUrl({
url: 'ModuleB/PageB'
});
router.pushUrl 还支持传递参数,例如:
router.pushUrl({
url: 'ModuleB/PageB',
params: {
key1: 'value1',
key2: 'value2'
}
);
在PageB中,可以通过router.getParams获取传递的参数:
const params = router.getParams();
console.log(params.key1); // 输出 'value1'
console.log(params.key2); // 输出 'value2'
如果目标页面需要返回结果,可以使用router.pushUrl的callback参数:
router.pushUrl({
url: 'ModuleB/PageB',
callback: (result) => {
console.log(result);
}
);
在PageB中,可以使用router.back返回结果:
router.back({
result: '返回结果'
);
router.pushUrl 还支持设置跳转动画:
router.pushUrl({
url: 'ModuleB/PageB',
anim: {
duration: 300,
curve: 'easeInOut'
}
);
以上是router.pushUrl在不同模块之间跳转的基本用法。
在HarmonyOS鸿蒙Next中,router.pushUrl用于页面之间的跳转。不同模块之间的跳转可以通过指定目标页面的路径来实现。假设模块A和模块B分别有不同的页面,你可以通过以下方式实现跳转:
import router from '@ohos.router';
// 从模块A跳转到模块B的页面
router.pushUrl({
url: 'pages/moduleB/PageB' // 目标页面的路径
}).then(() => {
console.log('跳转成功');
}).catch(err => {
console.error('跳转失败', err);
});
确保目标页面的路径正确,并且在模块配置中已正确声明。


