欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

Vue.use做了什么? - 新聞資訊 - 云南小程序開(kāi)發(fā)|云南軟件開(kāi)發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開(kāi)發(fā)/軟件開(kāi)發(fā)

知識(shí)

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X(jué)表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷(xiāo)的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷(xiāo)工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!

您當(dāng)前位置>首頁(yè) » 新聞資訊 » 技術(shù)分享 >

Vue.use做了什么?

發(fā)表時(shí)間:2020-10-18

發(fā)布人:葵宇科技

瀏覽次數(shù):62

Vue.use(js)

  • 該js如果是對(duì)象
    • 該對(duì)象里面要有一個(gè)install方法
    • Vue.use就是調(diào)用里面的install方法
  • 該js是一個(gè)function
    • Vue.use時(shí)function會(huì)直接執(zhí)行

作用

  • 可以在Vue的原型加一些東西
  • 注冊(cè)全局組件等

使用

  • 將hellow注冊(cè)為全局組件
  • 在原型中添加$num= 123
  1. 在components中新建components.js
import HelloWorld from '@/components/HelloWorld.vue'
export default {
  install: function (Vue) {
    // 這里的Vue就是你調(diào)用install方法時(shí)傳遞過(guò)來(lái)的
    // 可以在Vue原型中加一些東西
    Vue.prototype.$num = 123
    // 注冊(cè)全局組件
    Vue.component(HelloWorld.name, HelloWorld)
  }
}
  1. 在main.js中調(diào)用
import Vue from 'vue'
import App from './App.vue'
import components from '@/assets/components.js'
Vue.use(components)
Vue.config.productionTip = false

new Vue({
  render: h => h(App)
}).$mount('#app')

  1. Helloworld.vue
<template>
  <div>
    <h1>這里是HelloWord</h1>
    <h2>{{ $num }}</h2>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld'
}
</script>

<style></style>

結(jié)果:
**結(jié)果:**

  • 該js為對(duì)象時(shí),component.js寫(xiě)法不一樣,其他均一樣
export default function (Vue) {
  Vue.component(HelloWorld.name, HelloWorld)
  Vue.prototype.$num = 123
}

Vue.use(VueRouter)就是這么實(shí)現(xiàn)的

vue-router源碼

  function install (Vue) {
    if (install.installed && _Vue === Vue) { return }
    install.installed = true;

    _Vue = Vue;

    var isDef = function (v) { return v !== undefined; };

    var registerInstance = function (vm, callVal) {
      var i = vm.$options._parentVnode;
      if (isDef(i) && isDef(i = i.data) && isDef(i = i.registerRouteInstance)) {
        i(vm, callVal);
      }
    };

    Vue.mixin({
      beforeCreate: function beforeCreate () {
        if (isDef(this.$options.router)) {
          this._routerRoot = this;
          this._router = this.$options.router;
          this._router.init(this);
          Vue.util.defineReactive(this, '_route', this._router.history.current);
        } else {
          this._routerRoot = (this.$parent && this.$parent._routerRoot) || this;
        }
        registerInstance(this, this);
      },
      destroyed: function destroyed () {
        registerInstance(this);
      }
    });

    Object.defineProperty(Vue.prototype, '$router', {
      get: function get () { return this._routerRoot._router }
    });

    Object.defineProperty(Vue.prototype, '$route', {
      get: function get () { return this._routerRoot._route }
    });

    Vue.component('RouterView', View);
    Vue.component('RouterLink', Link);

    var strats = Vue.config.optionMergeStrategies;
    // use the same hook merging strategy for route hooks
    strats.beforeRouteEnter = strats.beforeRouteLeave = strats.beforeRouteUpdate = strats.created;
  }
Object.defineProperty(Vue.prototype, '$router', {
  get: function get () { return this._routerRoot._router }
});

Object.defineProperty(Vue.prototype, '$route', {
  get: function get () { return this._routerRoot._route }
});

其核心就是以上兩行代碼在install方法中將$router和route掛載到Vue原型上

相關(guān)案例查看更多