HarmonyOS鸿蒙Next中@ohos.router路由跳转

HarmonyOS鸿蒙Next中@ohos.router路由跳转

开发华为应用手表,链接真机上,使用 @ohos.router 跳转页面。跳转不成功,报错 Error message: Cannot read property push of undefined

  • 使用方式, 选择的API版本是8,华为手表watch 4pro 型号
import router from '@ohos.router';
handleClick() {
    console.log(router,'router')
    router.push({
        url:'pages/channelList/channelList'
    })
}

更多关于HarmonyOS鸿蒙Next中@ohos.router路由跳转的实战教程也可以访问 https://www.itying.com/category-93-b0.html

7 回复

楼主您好,关于路由跳转你可以参考以下文档中的第一个案例:

https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/start-with-ets-stage-0000001477980905-V3

有没有可能是这个文件没有配置:

更多关于HarmonyOS鸿蒙Next中@ohos.router路由跳转的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


src/main/config.json配置了路由,还是不行的,,

换API9试试看,

换API9试试看,

你可以了吗

楼主解决了吗,我也遇到这个问题

在HarmonyOS(鸿蒙)Next中,[@ohos](/user/ohos).router模块用于实现页面之间的路由跳转。该模块提供了多种方法来实现页面导航,包括pushreplaceback等。

  1. push方法:用于跳转到指定页面,并将当前页面压入路由栈中。示例代码如下:

    import router from '[@ohos](/user/ohos).router';
    router.push({
      url: 'pages/NextPage'
    });
    
  2. replace方法:用于跳转到指定页面,并替换当前页面,不将当前页面压入路由栈中。示例代码如下:

    import router from '[@ohos](/user/ohos).router';
    router.replace({
      url: 'pages/NextPage'
    });
    
  3. back方法:用于返回到上一个页面。示例代码如下:

    import router from '[@ohos](/user/ohos).router';
    router.back();
    
  4. clear方法:用于清空路由栈中的所有页面。示例代码如下:

    import router from '[@ohos](/user/ohos).router';
    router.clear();
    
  5. getLength方法:用于获取当前路由栈中的页面数量。示例代码如下:

    import router from '[@ohos](/user/ohos).router';
    let length = router.getLength();
    
  6. getState方法:用于获取当前路由栈的状态信息。示例代码如下:

    import router from '[@ohos](/user/ohos).router';
    let state = router.getState();
    

[@ohos](/user/ohos).router模块还支持传递参数,可以通过params属性将数据传递到目标页面。示例代码如下:

import router from '[@ohos](/user/ohos).router';
router.push({
  url: 'pages/NextPage',
  params: {
    key: 'value'
  }
);

在目标页面中,可以通过router.getParams方法获取传递的参数。示例代码如下:

import router from '[@ohos](/user/ohos).router';
let params = router.getParams();

[@ohos](/user/ohos).router模块提供了灵活的路由控制能力,适用于各种页面跳转场景。

在HarmonyOS鸿蒙Next中,@ohos.router模块提供了路由跳转功能,用于实现页面之间的导航。通过router.push方法可以跳转到指定页面,router.back用于返回上一页,router.replace则替换当前页面。路由跳转支持传递参数,便于页面间数据交互。开发者需在config.json中配置页面路由信息,确保跳转路径正确。

回到顶部