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

vue install 注冊(cè)全局組件方式

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

vue install注冊(cè)全局組件

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

具體使用的步驟如下

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

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文件中注冊(cè)

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

4.在頁(yè)面中使用

<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() 使用插件。

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

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

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

相關(guān)文章

最新評(píng)論