angular - Angular 中的脚本和使用 A-Frame 的打字稿的问题
问题描述
我对一段 HTML 代码的脚本有角度问题,该代码使用 A-Frame 制作 3D 对象并与之交互。问题是您可以将声音作为 a-frame 的属性来播放,但是当尝试脚本或从打字稿中它不起作用并且一切都已尝试时。我希望他们能帮助我
<script>
AFRAME.registerComponent('play', {
init: function () {
console.log("entro al script play");
var variable = document.querySelector('#yellow');
this.variable.addEventListener('click' , function(){
variable.components.sound.playSound();
});
}
});
AFRAME.registerComponent('stop', {
init: function () {
console.log("entro al script stop");
var variable = document.querySelector('#yellow');
this.variable.addEventListener('click' , function(){
variable.components.sound.stopSound();
});
}
});
<a-box id="yellow" color="yellow" position="0 0 -5" sound="src:#bichota">
<!-- Play -->
<a-sphere color="green" radius="0.25" position="-1 1 0" play></a-sphere>
<!-- Stop -->
<a-sphere color="red" radius="0.25" position="1 1 0" stop></a-sphere>
</a-box>
在打字稿中,我收到此错误“类型'元素'上不存在属性'组件'”
var entity= document.querySelector('#yellow') ;
if(entity != null){
console.log("enity" , entity);
entity.components.sound.playSound();
}
解决方案
要在 Angular 中使用 aframe,最好遵循 Angular 的做法,而不是直接涉及 DOM 操作。
如需完整演示,您可以在 Github 上克隆我的演示项目。
app.component.html
<a-scene>
<a-assets>
<audio id="bichota" src="../assets/test.mp3" preload="auto"></audio>
</a-assets>
<a-box id="yellow" color="yellow" position="0 0 -5" sound="src:#bichota" #yellowBox>
<a-sphere cursor="rayOrigin: mouse" color="green" radius="0.25" position="-1 1 0" (click)="playSound()"></a-sphere>
<a-sphere cursor="rayOrigin: mouse" color="red" radius="0.25" position="1 1 0" (click)="stopSound()"></a-sphere>
</a-box>
</a-scene>
#yellowBox
- 用于稍后在组件逻辑中引用它的角度cursor="rayOrigin: mouse"
- 用于通过鼠标单击元素。需要在 a-frame 元素中指定光标。有关详细信息,您可以阅读文档。(click)="xxxx()"
- 用于绑定点击事件
app.component.ts
export class AppComponent {
// refer to #yellowBox in the html
@ViewChild('yellowBox') yellowBox!: ElementRef;
playSound() {
console.log("play")
this.yellowBox.nativeElement.components.sound.playSound();
}
stopSound() {
console.log("stop")
this.yellowBox.nativeElement.components.sound.stopSound();
}
}
推荐阅读
- python - 网络机器人中特定方向的旋转
- html - 我的标签在我的下拉菜单中不起作用
- c++ - 重载 operator* 而不分配新内存
- r - 如何在 R 中已经有一些栅格文件的栅格堆栈中添加更多栅格?
- ruby - Ruby 只打印出类方法(而不是继承的方法)
- python - 如何在 Python 中从 JSON 获取有效的正则表达式?
- oracle - 不知道如何(Linux 命令)重新启动 oracle 数据库 12.1.0 服务器
- express - 如何在示例示例中从 ClusterBuster 矢量切片服务器接收切片数据?
- c# - ajax post 不返回视图数据
- sql-server - EF Core 3“更新”SQL 语句