You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * Convert an Array-like object to a real Array. */exportfunctiontoArray(list: any,start?: number): Array<any>{
start =start||0leti=list.length-startconstret: Array<any>=newArray(i)while(i--){ret[i]=list[i+start]}returnret}
Vue.use用法
vue
提供了Vue.use
的全局api
来注册插件,比如vuex
、vue-router
等用法
Vue.use(plugin)
这个方法需要在
new vue()
之前调用。Vue.use
会自动阻止多次注册相同插件,即使调用多次也只会注册一次。Vue.use源码分析
我们可以从源码入手分析一下,基于vue 2.6.11 版本,源码地址为:src/core/global-api/use.js
Vue.use
主要做了两件事检查插件是否已经注册,注册过得不需要重复注册
没有注册的,调用插件的
install
方法(参数是对象,则调用对象的install
方法,如果是函数,则直接当做install
方法调用), 同时将Vue
作为第一个参数传入Vue-Router中的 install
基于 vue-router3.1.6 版本,源码位置: src/install.js
vue-router
中的install
方法主要做了以下几件事:通过
mixin
混入的方式,如果是根组件,则直接把根组件的_router
设置为this.$options.router
如果不是根组件,那么递归往上找,直到找到根组件的,使用
_routerRoot
标记通过给
Vue.prototype
定义$router
、$route
属性后,使得所有的Vue实例(组件)都可以直接访问到$router
、$route
属性注册
<router-link>
、<router-view>
组件参考
The text was updated successfully, but these errors were encountered: