導航:首頁 > 文件教程 > vue組件中引入js文件

vue組件中引入js文件

發布時間:2023-10-17 22:13:03

『壹』 vue中導入外鏈css或js

在vue中導入自定義的css文件或js文件

css文件
在main.js中導入公共的css
import '../static/css/common.css' //引入公鏈族判用css

在頁面中引入單獨css
<style scoped>
@import '../../static/css/header.css';
</style>

Js文件

自定義js 中寫:
function title(t){
alert(t);
}

export {
title //多個方法在此處json中export出去
}

頁面中使用:
<template>
<div>
<button class="btn" @click="clickme">click me</button>
</div>
</template>
<script>
import {title} from './js/common.js' //可以選擇需棚改要的方法引入
export default {
data(){
return{
}
},
methods:{
clickme(){
title('你點我了穗碧');
}
}
}
</script>

或者

import Comjs from './js/common.js' //引入公用js
Vue.prototype.$comjs = Comjs; //添加到vue屬性中

自定義js 中寫:
const comjs = {

}

comjs.title = function (title) {
alert(title)
}

export default comjs;

頁面中使用:
<template>
<div>
<button class="btn" @click="clickme">click me</button>
</div>
</template>

<script>
export default {
data(){
return{
}
},
methods:{
clickme(){
this.$comjs.title('你點我了');
}
}
}
</script>

『貳』 vue項目中引入外部資源js

mounted() {

  const s = document.createElement('script');

  s.type = 'text/javascript';

  s.src = '你的需要的js文件地址';

  document.body.appendChild(s);

}

export default {

  components: {

    'remote-js': {

      render(createElement) {

        return createElement(

          'script',

          {

            attrs: {

              type: 'text/javascript',

              src: '你的需要的js文件地址',

            },

          },

        );

      },

    },

  },

}

閱讀全文

與vue組件中引入js文件相關的資料

熱點內容
網路數據用不成怎麼回事 瀏覽:135
applepayiphone5 瀏覽:71
word2010製作日歷 瀏覽:491
為什麼微信字顯示不全 瀏覽:761
香港蘋果官網怎麼預定iphone 瀏覽:844
文件目錄索引包括哪些 瀏覽:794
感情不要冷處理的微信個性簽名 瀏覽:54
機器人怎麼在電腦上編程 瀏覽:197
如何辨別購物的網站 瀏覽:13
喜歡編程嗎喜歡什麼語言 瀏覽:841
cad迷你看圖軟體怎麼刪除文件 瀏覽:267
看視頻寫文件格式 瀏覽:2
大數據平台怎麼審綠 瀏覽:398
java打包教程 瀏覽:787
電腦有多少個文件夾 瀏覽:1
dnf86版本極限光強 瀏覽:974
國考資料分析哪裡來的數據 瀏覽:87
文件拷貝到u盤以後md5變化 瀏覽:281
kali中pdf攻擊文件生成 瀏覽:507
專利檢索如何找到最接近對比文件 瀏覽:673

友情鏈接