亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將創建JS導入到 VueJS .vue 組件中?

如何將創建JS導入到 VueJS .vue 組件中?

嗶嗶one 2022-09-16 20:55:28
我提前道歉,總的來說,我對Vuejs還很陌生。我正在嘗試將創建JS / 聲音JS導入到.vue組件中。我已經通過 npm 安裝了創建JS。我只是不知道如何將庫導入組件,以便我可以引用聲音函數。我似乎無法在CreateJS文檔中找到任何用于此類用法的內容...任何代碼或參考鏈接將不勝感激。謝謝!
查看完整描述

1 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

好吧,我使用創建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 object


Vue.config.productionTip = false;

Vue.prototype.createjs = createjs; // Add the createjs instance to the Vue prototy, to use this.createjs


new Vue({

  render: h => h(App)

}).$mount("#app");

在 src/App.vue 組件(或任何組件,但 App.vue 是應用程序的入口點,因此它是執行此操作的好地方)中,配置聲音:


<template>

  <div id="app">

    <img alt="Vue logo" src="./assets/logo.png" />

    <HelloWorld msg="Welcome to Your Vue.js App" />

    <button @click="play">Play</button>

    <!-- button to call the play method -->

  </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 components


export default {

  name: "App",

  components: {

    HelloWorld

  },

  methods: {

    play() {

      this.createjs.Sound.play("Horn"); // Acces and play the sound with the id "Horn"

    }

  },

  created() {

    const soundId = "Horn"; // Id of the sound to be registered 

    this.createjs.Sound.registerSound(hornSound, soundId); // Register the sound, using the mp3 and the id

    // You can do this with any amount of sounds, here or in any component

    // Once a sound is registered, you have access to it in all the components

  }

};

</script>

播放來自子組件(src/組件/你好世界.vue)的聲音:


    <template>

      <div class="hello">

        <h3>Hello World with createjs/soundjs</h3>

        <button @click="playFromChild">Play inside child component</button>

      </div>

    </template>


    <script>

    export default {

      name: "HelloWorld",

      props: {

        msg: String

      },

      methods: {

        playFromChild() {

          this.createjs.Sound.play("Horn"); // We are accessing to the sound with id "Horn" without import anything

        }

      }

    };

    </script>

我希望這對您有所幫助,我試圖解釋如何使用它,但正如您所說,沒有關于它的文檔,所以也許這很棘手,但它有效。


查看完整回答
反對 回復 2022-09-16
  • 1 回答
  • 0 關注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號