導航:首頁 > 文件教程 > 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文件相關的資料

熱點內容
微信小程序按鈕顏色 瀏覽:69
長江大學網課用什麼app 瀏覽:431
華中系統圖紙編程哪個刀好 瀏覽:38
地方債務數據在哪裡查看 瀏覽:932
掃描文件怎麼設置格式 瀏覽:957
蘋果郵箱主機名填什麼 瀏覽:630
多張圖片同一個文件夾 瀏覽:798
win7怎麼打開shs文件 瀏覽:481
怎麼把文件夾做成iso 瀏覽:164
繽客網站上的房價怎麼在哪裡修改 瀏覽:406
單片機c51計數器實驗代碼 瀏覽:990
宏編程滑鼠代表什麼意思 瀏覽:753
別人撿到蘋果6有用嗎 瀏覽:829
word文件用wps打開 瀏覽:477
macbook修改文件格式軟體 瀏覽:757
美版s7edge那個版本好 瀏覽:529
視頻隱藏在文件夾里 瀏覽:144
網路通訊基礎是什麼 瀏覽:209
辦公電腦文件管理 瀏覽:222
火化費報銷文件有哪些 瀏覽:998

友情鏈接