【发布时间】:2021-10-31 17:04:57
【问题描述】:
我正在将一个项目从 Vue2 迁移到 3,并试图了解在组件数据上创建字段时遇到的错误。使用 ToneJS,我可以在方法体中或在 created 钩子中创建一个 new Synth(),然后在我的 playNote 方法中调用它。但是,当合成器被定义为data 上的字段时,单击“播放”按钮时出现以下错误。
在 Vue2 中我没有这样的问题。想知道是否有人可以解释这里发生了什么?为什么创建的钩子有效,而数据字段无效?
感谢您对此的任何帮助!错误全文:
runtime-core.esm-bundler.js?5c40:6568 [Vue 警告]:执行本机事件处理程序期间出现未处理错误
在
<template>
<div>
<button v-on:click="playNote">Play</button>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import * as Tone from 'tone'
export default defineComponent({
name: "Main",
data() {
return {
synth: new Tone.Synth().toDestination(), // this throws an error
playing: false,
}
},
methods: {
async playNote(){
console.log('playNote'); // logs properly await Tone.start();
console.log('this.synth', this.synth); // synth object logs
this.synth.triggerAttackRelease('C4', '8n');
// this works:
// const synth = new Tone.Synth().toDestination();
// synth.triggerAttackRelease('C4', '8n');
},
},
created() {
// this.synth = new Tone.Synth().toDestination() // this also works
}
});
</script>
【问题讨论】: