HarmonyOS鸿蒙Next中2in1应用页面跳转Ability方式报错Error message:No export named 'default' which exported by '&entry/src/main/ets/pages/TestGrid&

HarmonyOS鸿蒙Next中2in1应用页面跳转Ability方式报错Error message:No export named ‘default’ which exported by '&entry/src/main/ets/pages/TestGrid& 页面test1,点击按钮,跳转到TestGrid页面

报错Error message:No export named ‘default’ which exported by ‘&entry/src/main/ets/pages/TestGrid&’

单独运行TestGrid是正常的

test1.ets

private navigateToMainForm() {
  let want: Want = {
    bundleName: 'com.example.ztdigcen',
    abilityName: 'TestGrid',
    moduleName: 'entry' // 添加 moduleName(通常是 'entry')
  };

  this.context.startAbility(want).then(() => {//这里报错
    console.info('Start main form ability successfully');
    this.context.terminateSelf().then(() => {
      console.info('Login ability terminated successfully');
    });
  }).catch((err: BusinessError) => {
    console.error(`Failed to start main form ability. Code: ${err.code}, message: ${err.message}`);
    promptAction.showToast({
      message: '跳转失败,请重试',
      duration: 2000
    });
  });
}

module.json5

{
  "module": {
    "name": "entry",
    "type": "entry",
    "description": "$string:module_desc",
    "mainElement": "EntryAbility",
    "deviceTypes": [
      "2in1"
    ],
    "deliveryWithInstall": true,
    "installationFree": false,
    "pages": "$profile:main_pages",
    "abilities": [
      {
        "name": "EntryAbility",
        "srcEntry": "./ets/entryability/EntryAbility.ets",
        "description": "$string:EntryAbility_desc",
        "icon": "$media:layered_image",
        "label": "$string:EntryForm_label",
        "startWindowIcon": "$media:startIcon",
        "startWindowBackground": "$color:start_window_background",
        "exported": true,
        "skills": [
          {
            "entities": [
              "entity.system.home"
            ],
            "actions": [
              "ohos.want.action.home"
            ]
          }
        ]
      }
    ,{
        "name": "TestGrid",
        "srcEntry": "./ets/pages/TestGrid.ets",
        "description": "$string:EntryAbility_desc",
        "icon": "$media:layered_image",
        "label": "$string:EntryForm_label",
        "startWindowIcon": "$media:startIcon",
        "startWindowBackground": "$color:start_window_background",
        "exported": true,
        "skills": [
          {
            "actions": [
              "action.system.start"
            ]
          }
        ]
      }
    ],
    "extensionAbilities": [
      {
        "name": "EntryBackupAbility",
        "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets",
        "type": "backup",
        "exported": false,
        "metadata": [
          {
            "name": "ohos.extension.backup",
            "resource": "$profile:backup_config"
          }
        ],
      }
    ],
    "requestPermissions": [
      {
        "name": "ohos.permission.INTERNET",
        "usedScene": { "when": "always" }
      }
    ]
  }
}

更多关于HarmonyOS鸿蒙Next中2in1应用页面跳转Ability方式报错Error message:No export named 'default' which exported by '&entry/src/main/ets/pages/TestGrid&的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

TestGrid这个组件有被导出吗

更多关于HarmonyOS鸿蒙Next中2in1应用页面跳转Ability方式报错Error message:No export named 'default' which exported by '&entry/src/main/ets/pages/TestGrid&的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


该错误表明在TestGrid页面模块中未找到默认导出。在HarmonyOS Next中,页面跳转需要页面文件使用export default导出页面组件。请检查TestGrid.ets文件,确保页面组件已通过export default正确导出。

这个错误是因为在HarmonyOS Next中,TestGrid被配置为了一个Ability,但其源文件TestGrid.ets缺少必要的Ability入口导出。

从你的配置看,TestGridmodule.json5中被声明为一个Ability:

{
  "name": "TestGrid",
  "srcEntry": "./ets/pages/TestGrid.ets",
  // ... 其他配置
}

TestGrid.ets文件很可能只是一个普通的页面组件,没有按照Ability的要求导出默认类。在HarmonyOS Next中,每个Ability必须导出一个默认的类,这个类需要继承UIAbility或相关的Ability基类。

解决方案:

  1. 检查TestGrid.ets文件结构: 确保TestGrid.ets文件有正确的Ability结构:

    import UIAbility from '[@ohos](/user/ohos).app.ability.UIAbility';
    import window from '[@ohos](/user/ohos).window';
    
    export default class TestGridAbility extends UIAbility {
      onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
        // Ability创建时的初始化
      }
      
      onWindowStageCreate(windowStage: window.WindowStage): void {
        // 窗口阶段创建,加载页面
        windowStage.loadContent('pages/TestGrid', (err, data) => {
          if (err.code) {
            // 错误处理
          }
        });
      }
      
      // 其他生命周期方法...
    }
    
  2. 如果TestGrid只是普通页面: 如果TestGrid原本只是普通页面而不是独立的Ability,你需要调整跳转方式:

    • 使用页面路由跳转(如果是在同一个Ability内):
    router.pushUrl({
      url: 'pages/TestGrid'
    })
    
    • 或者修改module.json5配置,将TestGrid从abilities列表中移除
  3. 确认Ability类型: 检查TestGrid是否需要作为独立Ability。从你的want配置看,你试图启动一个名为TestGrid的Ability,因此必须确保该文件确实是一个完整的Ability实现。

错误信息No export named 'default'明确指出了问题:系统在TestGrid.ets文件中找不到默认导出,而这是Ability必须提供的。

回到顶部