vue2.x利用Vue.prototype注册全局组件/方法

简介: vue2.x利用Vue.prototype注册全局组件/方法

vue2.x利用Vue.prototype注册全局方法/变量/组件


有些时候我们需要在vue项目的任意位置都能调用公共方法/变量,那么vue如何注册全局的方法/变量/组件呢?请看以下示例:

  1. 创建common.js(编写插件),插件内利用Vue.prototype全局挂载方法
import { Toast } from 'vant';
import echarts from 'echarts';
const install = (Vue) => {
  // toast提示,通过this.Toast调用
  Vue.prototype.Toast = Toast;
  // echarts,通过this.$echarts调用
  Vue.prototype.$echarts = echarts;
  // 获取url参数
  Vue.prototype.$getUrlParam = (name, href) => {
    const _search = href ? href.split('?')[1] : window.location.search.substr(1);
    if (_search) {
      const _reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`, 'i');
      const _regNext = _search.match(_reg);
      if (_regNext) {
        return decodeURI(_regNext[2]) || '';
      }
      return '';
    }
    return '';
  };
  1. main.js(通过Vue.use注册刚刚编写的自定义插件),关于Vue.use的介绍,可以参考Vue.use的使用和基本原理
import Vue from 'vue';
import App from './App';
import router from './router';
import store from './store';
// 引入自定义插件
import common from './lib/common';
Vue.use(common); // 全局配置项
/* eslint-disable no-new */
// 把vue实例挂载在window.vm,方便使用vue的实例
window.vm = new Vue({
  el: '#app',
  router,
  store,
  components: {
    App,
  },
  template: '<App/>',
});
  1. 在任意组件中使用刚刚通过Vue.prototype全局注册的方法,比如使用$getUrlParam方法,直接通过this.$getUrlParam即可调用
<template>
  <div class="page">
  Vue.prototype全局注册的方法测试
  </div>
</template>
<script>
export default {
  name: 'Test',
  data() {},
  created() {
    console.log(this.$getUrlParam('idCard'))
  },
};
</script>
  1. 在任意js文件中使用刚刚通过Vue.prototype全局注册的方法,比如使用$getUrlParam方法。因为我们已经在main.js中将vue实例挂载在window.vm,所以在js文件中,直接通过window.vm.$getUrlParam即可调用
console.log(window.vm.$getUrlParam('idCard'))

当然,如果不想通过编写组件然后Vue.use的方式引入全局方法,也可以直接在main.js通过Vue.prototype注册全局方法/变量/组件。

mian.js:

import Vue from 'vue';
import App from './App';
import router from './router';
import store from './store';
  
// 通过Vue.prototype注册全局方法/变量/组件
// toast提示,通过this.Toast调用
Vue.prototype.Toast = Toast;
// echarts,通过this.$echarts调用
Vue.prototype.$echarts = echarts;
// 获取url参数
Vue.prototype.$getUrlParam = (name, href) => {
  const _search = href ? href.split('?')[1] : window.location.search.substr(1);
  if (_search) {
    const _reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`, 'i');
    const _regNext = _search.match(_reg);
    if (_regNext) {
      return decodeURI(_regNext[2]) || '';
    }
    return '';
  }
  return '';
};
  
// 把vue实例挂载在window.vm,方便使用vue的实例
window.vm = new Vue({
  el: '#app',
  router,
  store,
  components: {
  App,
  },
  template: '<App/>',
});

需要注意的是,只有vue2.x支持Vue.prototype注册全局方法/变量/组件,vue3.x不再支持Vue.prototype

目录
相关文章
|
Oracle 关系型数据库 数据库
人大金仓数据库使用uuid
人大金仓数据库使用uuid
人大金仓数据库使用uuid
|
前端开发 JavaScript 关系型数据库
若依框架------后台路由数据是如何转换为前端路由信息的
若依框架------后台路由数据是如何转换为前端路由信息的
2766 0
|
SQL 索引
在 SQL Server 中使用 STRING_AGG 函数
【8月更文挑战第5天】
5211 2
在 SQL Server 中使用 STRING_AGG 函数
|
前端开发 容器
彻底理解粘性定位 - position: sticky
彻底理解粘性定位 - position: sticky
1567 154
通过patch-package创建补丁修改node_modules依赖下的源码
通过patch-package创建补丁修改node_modules依赖下的源码
815 0
|
人工智能 云计算
阿里云AI技术亮相春晚!独家揭秘背后的故事!
阿里云AI技术亮相春晚!独家揭秘背后的故事!
|
SQL Oracle 关系型数据库
SQL 中的大小写处理函数详解
【8月更文挑战第31天】
1370 1
成功解决:Could not resolve dependency: npm ERR! peer vue@“^3.0.2“ from vuex@4.0.2
这篇文章讨论了在使用npm安装依赖时遇到的一个常见问题,即无法解析依赖导致的"peer dependency"冲突错误。文章提供了几种解决方法,包括清除npm缓存、删除`node_modules`文件夹和`package-lock.json`文件,然后重新尝试安装,以解决版本冲突问题。
|
数据安全/隐私保护
超级弱口令检查工具
超级弱口令工具在检测系统漏洞中的作用,包括如何使用和自定义设置。
4439 2