如何传递对框架组件的引用?
How to pass a reference to aframe component?
我正在编写一个自定义 aframe 组件来渲染基于非常长的对象数组的网格。
Aframe 文档仅将数组列为输入类型,您可以在其中传递属性并将其解析为数组attributename="1 2 3"
我想从外部将 javascript 引用传递给组件,如下所示:
const hugeArray = [{somejson}, ...]
const element = document.createElement('my-component');
element.<something happens here>
或者在 DOM API 之外创建一个组件,并将参数传递给组件的 init
方法。
找到了一种方法。
const hugeArray = [{somejson}, ...]
const element = document.createElement('a-entity');
element.setAttribute('mycomponent', '');
//... add component to DOM and let it initialize
element.components.mycomponent.customMethod(hugeArray);
所有假设组件都以名称 "mycomponent" 注册,并且在 init 等旁边有一个方法 customMethod
使用setAttribute
,它也可以接受对象和数组。通过 schema
而不是调用方法,因为 init
处理程序会在正确的时间自动为您调用。
https://aframe.io/docs/0.5.0/core/entity.html#setattribute-attr-value-componentattrvalue
AFRAME.registerComponent('mycomponent', {
schema: {
yourData: {type: 'array'}
},
init: function () {
console.log(this.data.yourData);
}
});
const hugeArray = [{somejson}, ...]
const element = document.createElement('a-entity');
element.setAttribute('mycomponent', {yourData: hugeArray});
scene.appendChild(element);
我正在编写一个自定义 aframe 组件来渲染基于非常长的对象数组的网格。
Aframe 文档仅将数组列为输入类型,您可以在其中传递属性并将其解析为数组attributename="1 2 3"
我想从外部将 javascript 引用传递给组件,如下所示:
const hugeArray = [{somejson}, ...]
const element = document.createElement('my-component');
element.<something happens here>
或者在 DOM API 之外创建一个组件,并将参数传递给组件的 init
方法。
找到了一种方法。
const hugeArray = [{somejson}, ...]
const element = document.createElement('a-entity');
element.setAttribute('mycomponent', '');
//... add component to DOM and let it initialize
element.components.mycomponent.customMethod(hugeArray);
所有假设组件都以名称 "mycomponent" 注册,并且在 init 等旁边有一个方法 customMethod
使用setAttribute
,它也可以接受对象和数组。通过 schema
而不是调用方法,因为 init
处理程序会在正确的时间自动为您调用。
https://aframe.io/docs/0.5.0/core/entity.html#setattribute-attr-value-componentattrvalue
AFRAME.registerComponent('mycomponent', {
schema: {
yourData: {type: 'array'}
},
init: function () {
console.log(this.data.yourData);
}
});
const hugeArray = [{somejson}, ...]
const element = document.createElement('a-entity');
element.setAttribute('mycomponent', {yourData: hugeArray});
scene.appendChild(element);