vue.use为注册全局插件所用,接收函数或者一个包含install属性的对象为参数;如果参数带有install就执行install,如果没有就直接将参数当install执行;并且第一个参数始终为vue对象,注册过的插件不会重新注册。

本教程操作环境:windows7系统、vue2.0版本、Dell G3电脑。
相关推荐:《vue.js教程》
定义
vue.use()往全局注入一个插件,供全局真接使用, 不需要单独引用
立即学习“前端免费学习笔记(深入)”;
代码理解:
大高朋团购系统是一套Groupon模式的开源团购程序,开发的一套网团购程序,系统采用ASP+ACCESS开发的团购程序,安装超简,功能超全面,在保留大高朋团购系统版权的前提下,允许所有用户免费使用。大高朋团购系统内置多种主流在线支付接口,所有网银用户均可无障碍支付;短信发送团购券和实物团购快递发货等。 二、为什么选择大高朋团购程序系统? 1.功能强大、细节完善 除了拥有主流团购网站功能,更特别支
import Router from 'vue-router' // 入口文件全局注入vue-router, 从而可以在全局使用this.$route Vue.use(Router)
如果不使用vue.use那么需在组件中使用都得单独引入
// a.vue import Router from 'vue-router' // b.vue import Router from 'vue-router'
理解了其基本使用及作用,我们来看一下vue.use中都发生了什么
源码很少,所以直接摘抄了
Vue.use = function (plugin: Function | Object) {
const installedPlugins = (this._installedPlugins || (this._installedPlugins = []))
if (installedPlugins.indexOf(plugin) > -1) {
return this
}
// additional parameters
const args = toArray(arguments, 1)
args.unshift(this)
if (typeof plugin.install === ‘function’) {
plugin.install.apply(plugin, args)
} else if (typeof plugin === ‘function’) {
plugin.apply(null, args)
}
installedPlugins.push(plugin)
return this
}总结
vue.use()为注册全局插件所用,接收函数或者一个包含install属性的对象为参数,如果参数带有install就执行install, 如果没有就直接将参数当install执行, 第一个参数始终为vue对象, 注册过的插件不会重新注册。









