我有以下使用组件初始化实体的简单示例:
<!DOCTYPE html>
<html>
<head>
<title>Hello, WebVR! - A-Frame</title>
<meta name="description" content="Hello, WebVR! - A-Frame">
<script src="https://aframe.io/releases/0.7.0/aframe.min.js"></script>
</head>
<script>
AFRAME.registerComponent('a', {
dependencies: ['b']
});
// Initializes second.
AFRAME.registerComponent('b', {
dependencies: ['c']
});
// Initializes first.
AFRAME.registerComponent('c', {});
</script>
<body>
<a-scene>
</a-scene>
</body>
<script>
sceneEl = document.querySelector('a-scene');
aEntity = document.createElement('a-entity');
aEntity.setAttribute('a');
sceneEl.appendChild(aEntity);
</script>
</html>
这来自于Aframe关于组件和依赖项的文档。
依赖关系:如果组件依赖于一个或多个其他组件,则可以控制组件初始化的顺序。在初始化当前组件之前,将从左到右初始化依赖项数组中指定的组件名称。如果依赖项具有其他依赖项组件,则这些其他依赖项组件将以相同的方式排序。
我的问题是为什么这个代码不起作用。代码按预期的方式生成a-entity
,但没有附加组件。我希望看到a、b和c附在我的实体上。我做错了什么?
发布于 2018-01-16 19:42:04
看起来,如果您不为setAttribute提供一个值,它就会被忽略。
试一试aEntity.setAttribute('a', '');
。
控制台应该显示:<a-entity c="" b="" a="" position="" rotation="" scale="" visible=""></a-entity>
<!DOCTYPE html>
<html>
<head>
<title>Hello, WebVR! - A-Frame</title>
<meta name="description" content="Hello, WebVR! - A-Frame">
<script src="https://aframe.io/releases/0.7.0/aframe.min.js"></script>
</head>
<script>
AFRAME.registerComponent('a', {
dependencies: ['b']
});
// Initializes second.
AFRAME.registerComponent('b', {
dependencies: ['c']
});
// Initializes first.
AFRAME.registerComponent('c', {});
</script>
<body>
<a-scene>
</a-scene>
</body>
<script>
sceneEl = document.querySelector('a-scene');
aEntity = document.createElement('a-entity');
aEntity.setAttribute('a', '');
sceneEl.appendChild(aEntity);
console.log(aEntity)
</script>
</html>
https://stackoverflow.com/questions/48292543
复制