如何传递对框架组件的引用? [英] How to pass a reference to aframe component?

查看:59
本文介绍了如何传递对框架组件的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个自定义aframe组件,以基于非常长的对象数组渲染网格。

I'm writing a custom aframe component to render a mesh based on a very long array of objects.

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

Aframe documentation only lists array as an input type where you can pass an attribute and it will be parsed into an array attributename="1 2 3"

我想通过以下方式将javascript引用从外部传递到组件中:

I would like to pass a javascript reference into the component from the outside with something like this:

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

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

or create a component outside of DOM API and pass arguments to component's init method.

推荐答案

使用 setAttribute ,它也可以采用对象和数组。通过 schema 而不是调用方法,因为 init 处理程序将在正确的时间自动为您调用。

Use setAttribute, which can take objects and arrays as well. Go through the schema rather than calling a method since the init handler will automatically get called for you at the right time.

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

这篇关于如何传递对框架组件的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆