简体   繁体   English

错误TS2345:类型为'{x:number; y:数字; z:数字; }'不可分配给'string'类型的参数

[英]error TS2345: Argument of type '{ x: number; y: number; z: number; }' is not assignable to parameter of type 'string'

I am trying to create a-image dynamically with the values received. 我正在尝试使用接收到的值动态创建一个图像。 But somehow I am getting below error while setting the position attribute of the imgObj(code below) 但是以某种方式我在设置imgObj的position属性时出现下面的错误(下面的代码)

xpos, ypos and zpos contains string floating values like xpos = "-0.2", ypos="-0.1", zpos="0.2" xpos, yposzpos包含字符串浮动值,例如xpos = "-0.2", ypos="-0.1", zpos="0.2"

var imgObj = document.createElement("a-image");
imgObj.setAttribute("src",'url(' + $event.src + ')');
//imgObj.setAttribute("position",{x:0,y:0,z:0})
imgObj.setAttribute('position',{x:xpos, y:ypos, z:zpos});

Any idea or pointers will help...thanks 任何想法或建议都会有所帮助...谢谢

If you were not using aframe, that would have been the correct behaviour from TypeScript, because in DOM / HTML only world, you'd get unexpected results from giving an object as the value of setAttribute . 如果您没有使用Aframe,那将是TypeScript的正确行为,因为在仅DOM / HTML的世界中,将对象作为setAttribute的值会得到意外的结果。

To tell TypeScript about the extra capabilities aframe adds, you can install the aframe typings: 要告知TypeScript aframe所增加的额外功能,您可以安装aframe类型:

npm install -D @types/aframe

This will add the typings that say the setAttribute can take an object with x,y,z numbers, when the the attribute is 当属性为时,这将添加如下类型: setAttribute可以接受带有x,y,z数字的对象。

Quoting from the typings https://unpkg.com/@types/aframe@0.8.0/index.d.ts : 从键入内容引用https://unpkg.com/@types/aframe@0.8.0/index.d.ts

export interface Coordinate {
    x: number;
    y: number;
    z: number;
}

Then lower in the file: 然后在文件中降低:

setAttribute(type: 'position' | 'rotation' | 'scale', value: Coordinate): void;

So, looks like it should do it. 因此,看起来应该这样做。

Final thought 最后的想法

It's worth noting that using setAttribute for this is not recommended by aframe, as shown in their documentation: https://aframe.io/docs/0.8.0/components/position.html 值得注意的是,aframe不建议为此使用setAttribute ,如其文档中所示: https ://aframe.io/docs/0.8.0/components/position.html

For performance and ergonomics, we recommend updating position directly via the three.js Object3D .position Vector3 versus via .setAttribute. 为了提高性能和人机工程学,我们建议直接通过three.js Object3D .position Vector3与.setAttribute来更新位置。

This method is easier because we have access to all the Vector3 utilities, and faster by skipping .setAttribute overhead and not needing to create an object to set rotation: 此方法更容易,因为我们可以访问所有Vector3实用程序,并且通过跳过.setAttribute开销并且不需要创建对象来设置旋转即可更快地实现:

I guess the solution is don't use Typescript. 我想解决方案是不使用Typescript。 It doesn't expect DOM methods to be overridden. 它不希望DOM方法被覆盖。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 TS2345:类型为“字符串”的参数 | null' 不能分配给“数字”类型的参数 - error TS2345: Argument of type 'string | null' is not assignable to parameter of type 'number' 类型 'string' 不可分配给类型 '“x” | “是” | “z”'。 TS2345 - Type 'string' is not assignable to type '“x” | “y” | “z”'. TS2345 TS2345:类型X的参数不能分配给类型Y的参数 - TS2345: Argument of type X is not assignable to parameter of type Y React Router (v6) useNavigate TypeScript 错误:“TS2345:‘number’类型的参数不可分配给‘To’类型的参数” - React Router (v6) useNavigate TypeScript error: "TS2345: Argument of type 'number' is not assignable to parameter of type 'To'" TS2345:“数字|数字”类型的参数 未定义”不能分配给“数字”类型的参数 - TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number' 打字稿错误 TS2345 错误:TS2345:“缓冲区”类型的参数不可分配给“字符串”类型的参数 - Typescript error TS2345 Error: TS2345:Argument of type 'Buffer' is not assignable to parameter of type 'string' 我收到错误 TS2345:X 类型的参数不可分配给 Y 类型的参数 - I have got error TS2345: Argument of type X is not assignable to parameter of type Y 错误TS2345:类型为'(a:Test,b:Test)=>布尔| 1'不能分配给类型'(a:Test,b:Test)=> number'的参数 - error TS2345: Argument of type '(a: Test, b: Test) => boolean | 1' is not assignable to parameter of type '(a: Test, b: Test) => number' TS2345:“字符串”类型的参数不可分配给“从不”类型的参数 - TS2345: Argument of type 'string' is not assignable to parameter of type 'never' 错误TS2345:“菜单”类型的参数无法分配给类型参数 - error TS2345: Argument of type 'Menu' is not assignable to parameter of type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM