最近Harmony的应用开发课程在程序员的圈子里,还是很火的。
于是,看了看自己手机里,21年写的dome,有点跟不上时代了。感觉还是有必要重新捡起来一下,说不定后面恰饭用得上。
在学习HarmonyOS 应用程序框架的时候,对路由页面的跳转进行了实践,前面都挺好,进入项目目录中 entry > src > main > ets > pages > index.ets ,在pages 目录下创建第二个页面secondPage ,secondPage 页面正确创建完毕,一切都很丝滑。当我在index页面中 引入router,添加好注册好按钮点击跳转事件,代码如下:
import router from '@ohos.router';
import CommonConstants from '../constants/CommonConstants';
import Logger from '../utils/Logger';
const TAG = '[IndexPage]';
@Entry
@Component
struct Index {
@State message: string = CommonConstants.INDEX_MESSAGE;
build() {
Row() {
Column() {
Text(this.message)
.fontSize(CommonConstants.FONT_SIZE)
.fontWeight(FontWeight.Bold)
Blank()
Button($r('app.string.next'))
.fontSize(CommonConstants.BUTTON_FONT_SIZE)
.width(CommonConstants.BUTTON_WIDTH)
.height(CommonConstants.BUTTON_HEIGHT)
.backgroundColor($r('app.color.button_bg'))
.onClick(() => {
router.pushUrl({
url: CommonConstants.SECOND_URL,
params: {
src: CommonConstants.SECOND_SRC_MSG
}
},router.RouterMode.Single).catch((error: Error) => {
Logger.info(TAG, 'IndexPage push error' + JSON.stringify(error));
});
})
}
.width(CommonConstants.FULL_WIDTH)
.height(CommonConstants.LAYOUT_HEIGHT)
}
.height(CommonConstants.FULL_HEIGHT)
.backgroundColor($r('app.color.page_bg'))
}
}
然后点击按钮,按照路由进行跳转,出问题了,报错如下:
[manifest_router.cpp(GetPagePath)-(0)] [Engine Log] can't find this page pages
[page_router_manager.cpp(StartPush)-(0)] [Engine Log] this uri not support in route push.
提示:找不到路由文件。
开始以为是路由写错了,反复查看官方文档,确认路由无误。突然想起写小程序,一般都需要手动添加页面路由信息。于是:
找到工程目录下 entry > src > main > resources > base > profile > main_pages.json文件,添加pages/SecondPage。路由信息如下:
{
"src": [
"pages/Index",
"pages/SecondPage",
]
}
然后就可以完美跳转了