HarmonyOS鸿蒙Next中Argument of type 'string' is not assignable to parameter of type 'Configuration'. <ArkTSCheck>

HarmonyOS鸿蒙Next中Argument of type ‘string’ is not assignable to parameter of type ‘Configuration’. <ArkTSCheck> 怎么解决:Argument of type ‘string’ is not assignable to parameter of type ‘Configuration’. <ArkTSCheck>;另外Configuration 是什么类型

cke_2758.png


更多关于HarmonyOS鸿蒙Next中Argument of type 'string' is not assignable to parameter of type 'Configuration'. <ArkTSCheck>的实战教程也可以访问 https://www.itying.com/category-93-b0.html

8 回复

解决 createWindow 参数类型错误及 Configuration 类型说明

问题分析

错误信息显示您传递给 window.createWindow() 的参数类型不正确。第一个参数应该是 Configuration 类型,但您传递了一个字符串。

解决方案

1. 正确使用 createWindow 方法

根据鸿蒙文档,createWindow 方法需要传递一个 Configuration 对象作为第一个参数,而不是字符串:

import { window } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';

@Entry
@Component
struct Index {
  async aboutToAppear() {
    // 创建正确的 Configuration 对象
    let config: window.Configuration = {
      name: "demo", // 窗口名称
      windowType: window.WindowType.TYPE_FLOAT, // 窗口类型
      ctx: getContext() // 上下文对象,需要从UIAbility获取
    };

    try {
      const win = await window.createWindow(config);
      // 使用创建的窗口进行后续操作
    } catch (error) {
      console.error(`Failed to create window. Code: ${error.code}, message: ${error.message}`);
    }
  }

  build() {
    Column() {
      Text('Hello')
        .fontSize(30)
    }
  }
}

2. 在 UIAbility 中正确创建窗口

根据鸿蒙文档示例,创建窗口通常在 UIAbility 的 onWindowStageCreate 方法中进行:

// EntryAbility.ets
import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage: window.WindowStage): void {
    let config: window.Configuration = {
      name: "demo",
      windowType: window.WindowType.TYPE_FLOAT,
      ctx: this.context // 使用UIAbility的context
    };
    
    try {
      window.createWindow(config).then((win: window.Window) => {
        console.info('Succeeded in creating the window.');
        // 可以在这里进行窗口操作,如调整大小
        win.resize(500, 1000);
      }).catch((err: BusinessError) => {
        console.error(`Failed to create the window. Cause code: ${err.code}, message: ${err.message}`);
      });
    } catch (exception) {
      console.error(`Failed to create the window. Cause code: ${exception.code}, message: ${exception.message}`);
    }
    
    // 加载主窗口内容
    windowStage.loadContent("pages/Index", (err) => {
      if (err.code) {
        console.error('Failed to load the content. Cause:' + JSON.stringify(err));
        return;
      }
      console.info('Succeeded in loading the content.');
    });
  }
}

Configuration 类型说明

根据鸿蒙文档,Configuration 是一个接口类型,包含以下属性:

参数名 类型 必填 说明
name string 窗口名称
windowType WindowType 窗口类型
ctx Context 上下文对象
defaultDensityEnabled boolean 是否启用默认密度(可选)

WindowType 枚举值

  • TYPE_APP: 应用窗口
  • TYPE_SYSTEM_ALERT: 系统警告窗口
  • TYPE_TOAST: toast窗口
  • TYPE_DIALOG: 对话框窗口
  • TYPE_FLOAT: 浮动窗口

注意事项

  1. 创建 TYPE_FLOAT 类型窗口需要申请 ohos.permission.SYSTEM_FLOAT_WINDOW 权限
  2. 需要在 module.json5 中声明所需权限:
{
  "module": {
    "requestPermissions": [
      {
        "name": "ohos.permission.SYSTEM_FLOAT_WINDOW"
      }
    ]
  }
}

更多关于HarmonyOS鸿蒙Next中Argument of type 'string' is not assignable to parameter of type 'Configuration'. <ArkTSCheck>的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


根据其文档步骤来创建但是并未创建窗口成功 请问下是什么问题

文档里写着当前仅PC/2in1设备应用可申请此权限,你可以试试这两设备的模拟器/真机能不能用
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/restricted-permissions#ohospermissionsystem_float_window

如果你想判断某个能力在当前设备能不能用,可以用 canIUse 来判断
https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-syscap#caniuse

Argument of type ‘string’ is not assignable to parameter of type ‘Configuration’ 错误,表明代码中某个函数或方法期望接收 Configuration 类型的参数,但实际传递了一个字符串类型(string)。

解决方案:

构造正确的 Configuration 对象:确保传递的参数是符合 Configuration 结构的对象,而非字符串

// 错误:传递字符串
someFunction("configName");  

// 正确:传递 Configuration 对象
const config: Configuration = {
  field1: "value1",
  field2: 100
};
someFunction(config);

Argument of type 'string' is not assignable to parameter of type 'Configuration'. <ArkTSCheck>报错表示你试图将一个字符串类型的值赋给一个期望接收Configuration类型的参数。使用window.createWindow创建窗口时需要传入Configuration类型的参数,可以参考示例,构建Configuration类型参数传入window.createWindow中创建窗口:

let config: window.Configuration = {
  name: "test",
  windowType: window.WindowType.TYPE_DIALOG,
};
try {
  window.createWindow(config).then((value:window.Window) => {
    console.info('Succeeded in creating the window. Data: ' + JSON.stringify(value));
    value.resize(500, 1000);
  }).catch((err:BusinessError)=>{
    console.error(`Failed to create the window. Cause code: ${err.code}, message: ${err.message}`);
  });
} catch (exception) {
  console.error(`Failed to create the window. Cause code: ${exception.code}, message: ${exception.message}`);
}

看楼主的截图需要注意下创建window.WindowType.TYPE_FLOAT类型的窗口需要申请ohos.permission.SYSTEM_FLOAT_WINDOW权限

该错误表明在鸿蒙Next开发中,尝试将字符串类型赋值给期望Configuration类型的参数。Configuration是鸿蒙系统提供的配置对象接口,用于参数传递。需检查调用方法时的参数类型,确保传入的是Configuration对象而非字符串。可能涉及UI组件配置或系统服务调用时的类型不匹配问题。

这个错误表明你正在尝试将一个字符串传递给期望Configuration类型参数的函数或方法。Configuration通常是一个对象类型,包含特定的配置属性和结构,而不是简单的字符串。

要解决这个问题:

  1. 检查API文档确认Configuration类型的具体结构
  2. 将字符串参数转换为符合Configuration接口的对象
  3. 或者检查是否使用了错误的参数位置

例如,如果需要一个包含url属性的配置对象:

// 错误用法
someFunction("https://example.com");

// 正确用法
someFunction({ url: "https://example.com" });

请参考相关API文档来了解Configuration类型的具体定义和要求。

回到顶部