【问题标题】:How to pass a reference to aframe component?如何传递对框架组件的引用?
【发布时间】:2017-08-08 20:35:05
【问题描述】:

我正在编写一个自定义框架组件,以基于非常长的对象数组呈现网格。

Aframe 文档仅将数组列为输入类型,您可以在其中传递属性并将其解析为数组attributename="1 2 3"

我想从外部将 javascript 引用传递给组件,如下所示:

const hugeArray = [{somejson}, ...]
const element = document.createElement('my-component');
element.<something happens here>

或者在 DOM API 之外创建一个组件并将参数传递给组件的 init 方法。

【问题讨论】:

    标签: javascript components aframe


    【解决方案1】:

    找到了一种方法。

    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

    【讨论】:

      【解决方案2】:

      使用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);
      

      【讨论】:

      • 同意,这是一个更简洁的选择。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多