最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue install 注冊全局組件方式

 更新時(shí)間:2022年03月29日 09:16:44   作者:木槿之夏  
這篇文章主要介紹了vue install 注冊全局組件方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

vue install注冊全局組件

項(xiàng)目中的有些組件使用的頻率比較高,這時(shí)候我們可以注冊全局的組件,這樣就不用每次使用的組件的時(shí)候一次次的導(dǎo)入

具體使用的步驟如下

一般會把這個(gè)常用的組件在main.js文件中注冊好

1.首先先建立一個(gè)公用的組件

// Cmponent.vue 公用的組件
<template>
? ? <div>
? ? ? ? 我是組件
? ? </div>
</template>
<script>
? ? export default {?
? ? }
</script>
<style scoped>
? ? div{
? ? ? ? font-size:40px;
? ? ? ? color:#fbb;
? ? ? ? text-align:center;
?? ?}
</style>

2.新建一個(gè)install.js文件

import component from './Cmponent.vue'
const component = {
? ? install:function(Vue){
? ? ? ? Vue.component('component-name',component)
? ? } ?//'component-name'這就是后面可以使用的組件的名字,install是默認(rèn)的一個(gè)方法 component-name 是自定義的,我們可以按照具體的需求自己定義名字
}
// 導(dǎo)出該組件
export default component

3.在main.js文件中注冊

// 引入組件
import install from '@plugins/install';?
// 全局掛載utils
Vue.use(install);

4.在頁面中使用

<template>
? ?<div>
? ? ? <component-name></component-name>
? ?</div> ? ?
</template>

vue插件的install方法

MyPlugin.install = function (Vue, options) {
  // 1. 添加全局方法或?qū)傩?
  Vue.myGlobalMethod = function () {
    // 邏輯...
  }
  // 2. 添加全局資源
  Vue.directive('my-directive', {
    bind (el, binding, vnode, oldVnode) {
      // 邏輯...
    }
    ...
  })
  // 3. 注入組件
  Vue.mixin({
    created: function () {
      // 邏輯...
    }
    ...
  })
  // 4. 添加實(shí)例方法
  Vue.prototype.$myMethod = function (methodOptions) {
    // 邏輯...
  }
}
import Vue from 'vue'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)

這里注意的就是vue插件的使用方法,通過全局方法 Vue.use() 使用插件。

插件通常會為 Vue 添加全局功能。插件的范圍沒有限制——一般有下面幾種:添加全局方法或者屬性;添加全局資源:指令/過濾器/過渡等;通過全局 mixin 方法添加一些組件選項(xiàng);添加 Vue 實(shí)例方法,通過把它們添加到 Vue.prototype 上實(shí)現(xiàn)。

了解vue插件的install方法對我們寫大型項(xiàng)目有很大幫助。

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

相關(guān)文章

最新評論

无锡市| 崇信县| 伊宁市| 通榆县| 兴山县| 长沙县| 晋江市| 海城市| 保亭| 安达市| 南宫市| 长武县| 海晏县| 茶陵县| 牡丹江市| 阿坝| 鄢陵县| 明溪县| 南木林县| 莱州市| 金塔县| 利津县| 勐海县| 太仆寺旗| 宁明县| 托里县| 兴城市| 萍乡市| 北碚区| 昌吉市| 科技| 滨海县| 即墨市| 株洲市| 锦屏县| 康乐县| 台南市| 囊谦县| 三江| 亳州市| 东宁县|