亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue 全局引用公共的組件以及公共的JS文件問題

 更新時間:2022年09月22日 08:48:36   作者:圓唉_  
這篇文章主要介紹了vue 全局引用公共的組件以及公共的JS文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

全局引用公共的組件及公共的JS文件

1. 創(chuàng)建一個公共的目錄 timeline ,里面包含 timeline.js 和 timeline.vue 文件,timeline.vue 用來寫公共的頁面,timeline.js 用來導(dǎo)出這個組件。

在這里插入圖片描述

在這里插入圖片描述

timeline.vue 文件內(nèi)容如下

<template>
  <div>頁面展示內(nèi)容</div>
</template>
<script>
export default {
  data() {
    return {};
  },
  methods: {}
};
</script>
<style lang="less" scoped>
</style>

timeline.js 文件內(nèi)容如下

import timelineData from './timeline.vue';
const timeline = {
  install: (Vue) => {
  	// 注冊并獲取組件,然后在 main.js 中引入,并 Vue.use()掛載
    Vue.component('timeline', timelineData)
  }
};
export default timeline;

2. 在 main.js 中引入公共的文件并掛載到Vue中

...
// 引入timeline
import timeline from './timeline/timeline.js';
Vue.use(timeline);
...

3. 在需要用到 timeline 的組件文件中直接使用即可

<template>
  <div>
  	// 頁面中直接使用即可
	<timeline></timeline>
  </div>
</template>

全局引入自定義組件問題

文件目錄

1. 書寫組件

<!-- index.vue -->
<template>
  <button class="h-button" :type="type">
    <slot></slot>
  </button>
</template>
<script>
export default {
  props:{
    type:{
      type:String,
      default:'button'
    }
  },
  data(){
    return{
    }
  }
}
</script>

2. 暴露install()方法

// index.js
import HButton from './index.vue';
HButton.install=function(Vue){
  Vue.component('HButton',HButton) // (組件名稱,對應(yīng)組件)
}
export default HButton;

3. 全局注冊

// main.js
// @ is an alias to /src
import HButton from '@/components/Btn/index'
Vue.use(HButton)

4. 使用

<!-- Home.vue 使用 -->
<template>
  <div class="home">
    <h-button>組件使用</h-button>
  </div>
</template>
<script>
export default {
  name: "Home",
  components: {},
};
</script>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論