知識(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í)提供便捷的支持!
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
- 在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)
}
}
- 在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')
- Helloworld.vue
<template>
<div>
<h1>這里是HelloWord</h1>
<h2>{{ $num }}</h2>
</div>
</template>
<script>
export default {
name: 'HelloWorld'
}
</script>
<style></style>
結(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)案例查看更多
相關(guān)閱讀
- 網(wǎng)站開(kāi)發(fā)公司哪家好
- vue開(kāi)發(fā)小程序
- 買(mǎi)小程序被騙
- 排名
- 汽車(chē)報(bào)廢拆解管理系統(tǒng)
- 云南小程序開(kāi)發(fā)
- SEO
- 文山小程序開(kāi)發(fā)
- 報(bào)廢車(chē)拆解回收管理系統(tǒng)
- 楚雄網(wǎng)站建設(shè)公司
- 人口普查小程序
- 小程序開(kāi)發(fā)公司
- 云南etc微信小程序
- 云南網(wǎng)站設(shè)計(jì)
- 報(bào)廢車(chē)拆解軟件
- 云南網(wǎng)站開(kāi)發(fā)哪家好
- 云南網(wǎng)站建設(shè)首選公司
- uniapp開(kāi)發(fā)小程序
- 大理小程序開(kāi)發(fā)
- 高端網(wǎng)站建設(shè)公司
- 網(wǎng)站建設(shè)服務(wù)公司
- 云南軟件定制
- 生成海報(bào)
- 網(wǎng)站建設(shè)首選公司
- 報(bào)廢車(chē)回收管理軟件
- 云南小程序開(kāi)發(fā)公司
- 汽車(chē)回收管理
- 網(wǎng)站排名優(yōu)化
- 網(wǎng)站建設(shè)報(bào)價(jià)
- 搜索引擎排名