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

vue自定義全局組件(自定義插件)的用法

 更新時間:2018年01月30日 10:26:44   作者:Tank_in_the_street  
這篇文章主要介紹了vue自定義全局組件(自定義插件)的用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

有時候我們在做開發(fā)的時候,就想自己寫一個插件然后就可以使用自己的插件,那種成就感很強。博主最近研究element-ui和axios的時候,發(fā)現(xiàn)他們是自定義組件,但是唯一有一點不同的是,在用element-ui的時候是使用Vue.use()語句來使用的,而axios的時候,不用Vue.use(),只要import就可以導入進來了,感覺很神奇,細細的發(fā)現(xiàn),原來他們的不同是因為axios里面并沒有寫install方法,而element-ui就有寫這個方法,下面就利用這個install來寫一個自己的插件。

首先寫這個插件之前生成好一個目錄來存放這個插件。博主我是將他放在一個component的loading目錄下:

在該目錄下,按博主習慣是寫一個index.js文件還有一個組件loading.vue,index.js里面寫的是關于loading.vue的install方法。代碼如下所示:

import LoadingComponent from './Loading.vue'

const Loading={
  install:function (Vue) {
    Vue.component('Loading',LoadingComponent)
  }
}
export default Loading

其中install方法表示在main.js中,如果使用Vue.use()方法的話,則該方法默認會調用install方法。在install方法里面還注冊了組件,這里面'Loading'指的是外面App.vue使用的組件名,LoadingComponent指的是上面引過來的Loading.vue。之后通過export default Loading導出。

然后Loading.vue代碼如下所示:

<template>
  <div class="loading-box">
    Loading...
  </div>
</template>
<script></script>

Loading.vue代碼寫完后然后就在默認的main.js文件中導入,通過使用Vue.use()方法導入剛剛寫好的index.js:

import Vue from 'vue'
import App from './App.vue'
import Loading from './components/loading'
Vue.use(Loading)
new Vue({
 el: '#app',
 render: h => h(App)
})

然后就在App.vue方法里面使用該模板就可以了:

<template>
 <div id="app">
  <Loading></Loading>
 </div>
</template>

你也可以在剛剛的loading.vue文件里面寫自己的一些代碼,比如寫一個日歷插件,按鈕插件等等。如下面這個:

<template>
  <div class="loader">
    <div class="loader-inner ball-spin-fade-loader">
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
    </div>
  </div>
</template>
<style scoped>
  .loader{
    width:80px;
    height: 80px;
    margin:50px auto;
  }
  @keyframes ball-spin-fade-loader {
    50% {
      opacity: 0.3;
      -webkit-transform: scale(0.4);
      transform: scale(0.4); }

    100% {
      opacity: 1;
      -webkit-transform: scale(1);
      transform: scale(1); } }

  .ball-spin-fade-loader {
    position: relative; }
  .ball-spin-fade-loader > div:nth-child(1) {
    top: 25px;
    left: 0;
    -webkit-animation: ball-spin-fade-loader 1s 0s infinite linear;
    animation: ball-spin-fade-loader 1s 0s infinite linear; }
  .ball-spin-fade-loader > div:nth-child(2) {
    top: 17.04545px;
    left: 17.04545px;
    -webkit-animation: ball-spin-fade-loader 1s 0.12s infinite linear;
    animation: ball-spin-fade-loader 1s 0.12s infinite linear; }
  .ball-spin-fade-loader > div:nth-child(3) {
    top: 0;
    left: 25px;
    -webkit-animation: ball-spin-fade-loader 1s 0.24s infinite linear;
    animation: ball-spin-fade-loader 1s 0.24s infinite linear; }
  .ball-spin-fade-loader > div:nth-child(4) {
    top: -17.04545px;
    left: 17.04545px;
    -webkit-animation: ball-spin-fade-loader 1s 0.36s infinite linear;
    animation: ball-spin-fade-loader 1s 0.36s infinite linear; }
  .ball-spin-fade-loader > div:nth-child(5) {
    top: -25px;
    left: 0;
    -webkit-animation: ball-spin-fade-loader 1s 0.48s infinite linear;
    animation: ball-spin-fade-loader 1s 0.48s infinite linear; }
  .ball-spin-fade-loader > div:nth-child(6) {
    top: -17.04545px;
    left: -17.04545px;
    -webkit-animation: ball-spin-fade-loader 1s 0.6s infinite linear;
    animation: ball-spin-fade-loader 1s 0.6s infinite linear; }
  .ball-spin-fade-loader > div:nth-child(7) {
    top: 0;
    left: -25px;
    -webkit-animation: ball-spin-fade-loader 1s 0.72s infinite linear;
    animation: ball-spin-fade-loader 1s 0.72s infinite linear; }
  .ball-spin-fade-loader > div:nth-child(8) {
    top: 17.04545px;
    left: -17.04545px;
    -webkit-animation: ball-spin-fade-loader 1s 0.84s infinite linear;
    animation: ball-spin-fade-loader 1s 0.84s infinite linear; }
  .ball-spin-fade-loader > div {
    background-color: #399;
    width: 15px;
    height: 15px;
    border-radius: 100%;
    margin: 2px;
    -webkit-animation-fill-mode: both;
    animation-fill-mode: both;
    position: absolute; }
</style>

效果是一個滾動的圓:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Vue.sync修飾符與$emit(update:xxx)詳解

    Vue.sync修飾符與$emit(update:xxx)詳解

    這篇文章主要介紹了Vue.sync修飾符與$emit(update:xxx),實現(xiàn)思路非常簡單,文章介紹了.sync修飾符的作用和使用.sync修飾符的寫法,實現(xiàn)代碼簡單易懂對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-11-11
  • vue-router傳遞參數(shù)的幾種方式實例詳解

    vue-router傳遞參數(shù)的幾種方式實例詳解

    vue-router傳遞參數(shù)分為兩大類,一類是編程式的導航 router.push另一類是聲明式的導航 <router-link>,本文通過實例代碼給大家介紹vue-router傳遞參數(shù)的幾種方式,感興趣的朋友跟隨小編一起看看吧
    2018-11-11
  • vue如何解決數(shù)據加載時,插值表達式閃爍問題

    vue如何解決數(shù)據加載時,插值表達式閃爍問題

    這篇文章主要介紹了vue如何解決數(shù)據加載時,插值表達式閃爍問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • vue在App.vue文件中監(jiān)聽路由變化刷新頁面操作

    vue在App.vue文件中監(jiān)聽路由變化刷新頁面操作

    這篇文章主要介紹了vue在App.vue文件中監(jiān)聽路由變化刷新頁面操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 關于Vue3使用axios的配置教程詳解

    關于Vue3使用axios的配置教程詳解

    道axios是一個庫,并不是vue中的第三方插件,下面這篇文章主要給大家介紹了關于Vue3使用axios的配置教程,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-04-04
  • vue.config.js中configureWebpack與chainWebpack區(qū)別及說明

    vue.config.js中configureWebpack與chainWebpack區(qū)別及說明

    這篇文章主要介紹了vue.config.js中configureWebpack與chainWebpack區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • VUE組件中的 Drawer 抽屜實現(xiàn)代碼

    VUE組件中的 Drawer 抽屜實現(xiàn)代碼

    這篇文章主要介紹了VUE組件 之 Drawer 抽屜 ,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • vue實現(xiàn)組件通信的八種方法實例

    vue實現(xiàn)組件通信的八種方法實例

    ue是數(shù)據驅動視圖更新的框架, 所以對于vue來說組件間的數(shù)據通信非常重要,下面這篇文章主要給大家介紹了關于vue實現(xiàn)組件通信的八種方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-09-09
  • Vue + better-scroll 實現(xiàn)移動端字母索引導航功能

    Vue + better-scroll 實現(xiàn)移動端字母索引導航功能

    better-scroll 是一款重點解決移動端(已支持 PC)各種滾動場景需求的插件。這篇文章主要介紹了Vue + better-scroll 實現(xiàn)移動端字母索引導航功能,需要的朋友可以參考下
    2018-05-05
  • 詳解vuex狀態(tài)管理模式

    詳解vuex狀態(tài)管理模式

    這篇文章主要介紹了詳解vuex狀態(tài)管理模式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11

最新評論