猿问

如何将创建JS导入到 VueJS .vue 组件中?

我提前道歉,总的来说,我对Vuejs还很陌生。我正在尝试将创建JS / 声音JS导入到.vue组件中。我已经通过 npm 安装了创建JS。我只是不知道如何将库导入组件,以便我可以引用声音函数。我似乎无法在CreateJS文档中找到任何用于此类用法的内容...任何代码或参考链接将不胜感激。谢谢!


哔哔one
浏览 168回答 1
1回答

万千封印

好吧,我使用创建JS / SoundJS库从其CDN导入它做了一个示例。在 public/index.html 文件中,添加标记:<script src="https://code.createjs.com/1.0.0/soundjs.min.js"></script>现在,您的项目中有了库,并且可以访问它及其功能。在 src/main 中.js添加 Vue 要使用的库,将其添加到 Vue 原型中:import Vue from "vue";import App from "./App.vue";const createjs = window.createjs; // Get the createjs instance from window objectVue.config.productionTip = false;Vue.prototype.createjs = createjs; // Add the createjs instance to the Vue prototy, to use this.createjsnew Vue({&nbsp; render: h => h(App)}).$mount("#app");在 src/App.vue 组件(或任何组件,但 App.vue 是应用程序的入口点,因此它是执行此操作的好地方)中,配置声音:<template>&nbsp; <div id="app">&nbsp; &nbsp; <img alt="Vue logo" src="./assets/logo.png" />&nbsp; &nbsp; <HelloWorld msg="Welcome to Your Vue.js App" />&nbsp; &nbsp; <button @click="play">Play</button>&nbsp; &nbsp; <!-- button to call the play method -->&nbsp; </div></template><script>import HelloWorld from "./components/HelloWorld.vue";const hornSound = require("@/assets/hey.mp3"); // Store a mp3 file in a variable, You can add more sounds, here on in another componentsexport default {&nbsp; name: "App",&nbsp; components: {&nbsp; &nbsp; HelloWorld&nbsp; },&nbsp; methods: {&nbsp; &nbsp; play() {&nbsp; &nbsp; &nbsp; this.createjs.Sound.play("Horn"); // Acces and play the sound with the id "Horn"&nbsp; &nbsp; }&nbsp; },&nbsp; created() {&nbsp; &nbsp; const soundId = "Horn"; // Id of the sound to be registered&nbsp;&nbsp; &nbsp; this.createjs.Sound.registerSound(hornSound, soundId); // Register the sound, using the mp3 and the id&nbsp; &nbsp; // You can do this with any amount of sounds, here or in any component&nbsp; &nbsp; // Once a sound is registered, you have access to it in all the components&nbsp; }};</script>播放来自子组件(src/组件/你好世界.vue)的声音:&nbsp; &nbsp; <template>&nbsp; &nbsp; &nbsp; <div class="hello">&nbsp; &nbsp; &nbsp; &nbsp; <h3>Hello World with createjs/soundjs</h3>&nbsp; &nbsp; &nbsp; &nbsp; <button @click="playFromChild">Play inside child component</button>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </template>&nbsp; &nbsp; <script>&nbsp; &nbsp; export default {&nbsp; &nbsp; &nbsp; name: "HelloWorld",&nbsp; &nbsp; &nbsp; props: {&nbsp; &nbsp; &nbsp; &nbsp; msg: String&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; methods: {&nbsp; &nbsp; &nbsp; &nbsp; playFromChild() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.createjs.Sound.play("Horn"); // We are accessing to the sound with id "Horn" without import anything&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; </script>我希望这对您有所帮助,我试图解释如何使用它,但正如您所说,没有关于它的文档,所以也许这很棘手,但它有效。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答