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

熱點內容
把c盤的桌面文件移走了怎麼辦 瀏覽:949
2025第三代軍用北斗升級版 瀏覽:925
魅藍手勢密碼怎麼弄 瀏覽:509
蘋果store怎麼充值 瀏覽:224
note4應用程序許可設置在哪裡 瀏覽:489
路由器設置沒網路 瀏覽:539
微信無法載入視頻瀏覽器可以載入 瀏覽:419
linux配置環境 瀏覽:868
蘋果手機如何一頁只留幾個app 瀏覽:987
投標文件約定項包括哪些 瀏覽:920
如何學會人工智慧編程 瀏覽:889
手機qq傳的文件在哪 瀏覽:370
win10關閉數字證書 瀏覽:693
電腦字體放在哪個文件夾 瀏覽:27
開兩個相同版本cad 瀏覽:785
蘋果的文件用什麼打開方式 瀏覽:582
電腦文件的分類 瀏覽:598
2010word清除格式 瀏覽:221
怎麼把文件名插入excel 瀏覽:460
我的電腦文件排列 瀏覽:827

友情鏈接