javascript - 在 Vue 组件中导入和使用外部 Typescript 模块
问题描述
我对 Vue 很陌生,正在尝试在我的 Vue 组件中使用外部 Typescript 模块来渲染吉他指板(https://github.com/omnibrain/svguitar)。理想情况下,我希望能够在我的应用程序的不同位置呈现这些。我已按照中的步骤进行操作,ReadMe.md
但无法正常工作。这是我的组件的样子:
<template>
<div class="home">
<div class="chart" />
</div>
</template>
<script lang="ts">
import Vue from "vue";
import { SVGuitarChord } from "svguitar";
const chart = new SVGuitarChord("#chart");
export default Vue.extend({
mounted() {
console.log("I'm mounted");
chart
.configure({
strings: 6,
frets: 5
})
.chord({
fingers: [],
barres: []
})
.draw();
}
});
</script>
这样做,我在控制台中收到以下错误:
TypeError: Cannot read property 'put' of null
at Svg.addTo (svguitar.es5.js?98dc:2746)
at new SvgJsRenderer (svguitar.es5.js?98dc:7478)
at SVGuitarChord.get (svguitar.es5.js?98dc:7654)
at SVGuitarChord.clear (svguitar.es5.js?98dc:8021)
at SVGuitarChord.draw (svguitar.es5.js?98dc:7685)
at VueComponent.mounted (Home.vue?42b8:20)
at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1854)
at callHook (vue.runtime.esm.js?2b0e:4219)
at Object.insert (vue.runtime.esm.js?2b0e:3139)
at invokeInsertHook (vue.runtime.esm.js?2b0e:6346)
如果有人能帮我解决这个问题,那就太好了!如果需要任何其他信息,请告诉我。
解决方案
发生这种情况是因为您指定#chart
为容器,SVGuitarChord
但您编写<div class="chart" />
. 它应该是<div id="chart" />
推荐阅读
- android - Android Spinner 未填充 | 安卓科特林
- python - 我可以绕过将 self 对象传递给 python 类方法吗?
- indexing - 具有自定义索引的数组的“枚举”之类的函数?
- jenkins - 是否可以将 jenkins 管道编译成 jar 供共享库使用?
- c++ - 为什么要使用 getter 来构造私有成员而不是公开它们?
- javascript - 在两个单独的函数中或在单个函数中创建和填充对象?
- python - 如何在python中映射数组中的字符?
- python - 我正在尝试创建一个是或否对话树
- audiokit - AudioKit 播放破解
- laravel - Laravel 雄辩关系4表问题