[英]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, ypos
和zpos
包含字符串浮动值,例如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. 因此,看起来应该这样做。
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.