首先我们要知道
1.一个重要的内置关系:VueComponent.prototype.__proto__ === Vue.prototype,
即 VueComponent.prototype = new Vue()
2.为什么要有这个关系:让组件实例对象(vc)可以访问到 Vue原型上的属性、方法。
使用Vue.use会将
- 添加全局方法或者 property。如:vue-custom-element
- 添加全局资源:指令/过滤器/过渡等。如 vue-touch
- 通过全局混入来添加一些组件选项。如 vue-router
- 添加 Vue 实例方法,通过把它们添加到
Vue.prototype
上实现。- 一个库,提供自己的 API,同时提供上面提到的一个或多个功能
如下源码是vue-router的部分源码
//vue-router源码
import View from './components/view'
import Link from './components/link'
export let _Vue
export function install (Vue) {
if (install.installed && _Vue === Vue) return
install.installed = true
_Vue = Vue
const isDef = v => v !== undefined
const registerInstance = (vm, callVal) => {
let i