组件依赖如何在 A 框架中工作?
How do component dependencies work in A-frame?
我试图了解依赖项在 A 型框架组件中的工作方式,因为它似乎不像我想象的那样工作。
场景,我正在尝试创建一种“母舰”组件来存储全局信息,这些信息可以在体验的不同阶段从不同组件访问。为此,我将组件附加到 a-scene
,然后将“卫星”组件附加到该场景的子实体。
问题 是在加载母舰组件时似乎没有及时初始化以便访问其属性。我想如果我将母舰组件添加为卫星组件的 dependency
它会等待它被初始化但它似乎不起作用。
查看下面的组件;
//the mothership component
AFRAME.registerComponent('mothership', {
schema: {
testString: {type: 'string', default: 'Working'},
},
init: function(){
},
}),
//the satellite component
AFRAME.registerComponent('satellite', {
dependencies: ['mothership'],
init: function(){
var mother = this.el.sceneEl
var self = this.el
var message = mother.getAttribute('mothership').testString
//to demonstrate problem
self.setAttribute('value', message)
console.log(message)
//-> undefined
},
})
和HTML
<a-scene mothership>
<a-text
satellite
position="0 1.6 -3"
color="black"
align="center"
value="">
</a-text>
</a-scene>
Here is a fiddle 这个例子显示了它是如何不工作的,testString
的值是 undefined
.
我已经设法通过向等待母舰的 loaded
事件的卫星组件添加 eventListener
来建立这两个组件之间的连接。请参阅下面的示例和 fiddle.
//the mothership component
AFRAME.registerComponent('mothership', {
schema: {
testString: {type: 'string', default: 'Working'},
},
init: function(){
},
}),
//the satellite component
AFRAME.registerComponent('satellite', {
dependencies: ['mothership'],
init: function(){
var mother = this.el.sceneEl
var self = this.el
//wait for mothership to be loaded
mother.addEventListener('loaded', function(){
var message = mother.getAttribute('mothership').testString
//to demonstrate problem
self.setAttribute('value', message)
console.log(message)
})
},
})
和原来的例子一样HTML。 Here is a fiddle 的那个按我的预期工作。
那么dependencies
不就是等待指定的组件loaded/initialised吗?还是我做错了什么?如果有必要,我可以使用 eventListener 来解决这个问题,但我想更好地理解这一点,以及是否有更好的方法来实现这一点。
一如既往地感谢任何建议,如果您需要更多信息来理解问题,请告诉我。
组件依赖性适用于同一实体上的组件,而不是像您的卫星/母舰组件示例那样跨实体。像您一样使用加载的事件是可行的方法。
我试图了解依赖项在 A 型框架组件中的工作方式,因为它似乎不像我想象的那样工作。
场景,我正在尝试创建一种“母舰”组件来存储全局信息,这些信息可以在体验的不同阶段从不同组件访问。为此,我将组件附加到 a-scene
,然后将“卫星”组件附加到该场景的子实体。
问题 是在加载母舰组件时似乎没有及时初始化以便访问其属性。我想如果我将母舰组件添加为卫星组件的 dependency
它会等待它被初始化但它似乎不起作用。
查看下面的组件;
//the mothership component
AFRAME.registerComponent('mothership', {
schema: {
testString: {type: 'string', default: 'Working'},
},
init: function(){
},
}),
//the satellite component
AFRAME.registerComponent('satellite', {
dependencies: ['mothership'],
init: function(){
var mother = this.el.sceneEl
var self = this.el
var message = mother.getAttribute('mothership').testString
//to demonstrate problem
self.setAttribute('value', message)
console.log(message)
//-> undefined
},
})
和HTML
<a-scene mothership>
<a-text
satellite
position="0 1.6 -3"
color="black"
align="center"
value="">
</a-text>
</a-scene>
Here is a fiddle 这个例子显示了它是如何不工作的,testString
的值是 undefined
.
我已经设法通过向等待母舰的 loaded
事件的卫星组件添加 eventListener
来建立这两个组件之间的连接。请参阅下面的示例和 fiddle.
//the mothership component
AFRAME.registerComponent('mothership', {
schema: {
testString: {type: 'string', default: 'Working'},
},
init: function(){
},
}),
//the satellite component
AFRAME.registerComponent('satellite', {
dependencies: ['mothership'],
init: function(){
var mother = this.el.sceneEl
var self = this.el
//wait for mothership to be loaded
mother.addEventListener('loaded', function(){
var message = mother.getAttribute('mothership').testString
//to demonstrate problem
self.setAttribute('value', message)
console.log(message)
})
},
})
和原来的例子一样HTML。 Here is a fiddle 的那个按我的预期工作。
那么dependencies
不就是等待指定的组件loaded/initialised吗?还是我做错了什么?如果有必要,我可以使用 eventListener 来解决这个问题,但我想更好地理解这一点,以及是否有更好的方法来实现这一点。
一如既往地感谢任何建议,如果您需要更多信息来理解问题,请告诉我。
组件依赖性适用于同一实体上的组件,而不是像您的卫星/母舰组件示例那样跨实体。像您一样使用加载的事件是可行的方法。