欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue 全局引用公共的組件以及公共的JS文件問(wèn)題

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

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

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

在這里插入圖片描述

在這里插入圖片描述

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

<template>
  <div>頁(yè)面展示內(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) => {
  	// 注冊(cè)并獲取組件,然后在 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>
  	// 頁(yè)面中直接使用即可
	<timeline></timeline>
  </div>
</template>

全局引入自定義組件問(wèn)題

文件目錄

1. 書(shū)寫組件

<!-- 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) // (組件名稱,對(duì)應(yīng)組件)
}
export default HButton;

3. 全局注冊(cè)

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

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

相關(guān)文章

最新評(píng)論