vue2 vue3 全局变量和函数

前言

本篇文章主要介绍了 vue2 和 vue3的全局变量和函数应用,包括vue3 +ts 如何在 setup 中注册使用。


vue 2全局变量和函数

注意:可以注册多个多次没有限制

  1. 用法:在main.js中注册
//变量
Vue.prototype.msg= 'hello'
//函数
Vue.prototype.fn = function () {
		console.log('我是全局函数hello')
		}
  1. 在应用的任意组件模板上都可用,并且也可以通过任意组件实例的 this 访问到
<template>
 <div>
   {{msg}}
 </div>
</template>

export default {
    data () {
   	  return {
   	   msg: this.msg,
   	  }
    },
    mounted(){
    	console.log(this.msg)
    	this.fn()
    }
}

vue 3全局变量和函数

官网文档
app.config.globalProperties

一个用于注册能够被应用内所有组件实例访问到的全局属性的对象。

这是对 Vue 2 中 Vue.prototype 使用方式的一种替代,此写法在 Vue 3 已经不存在了。与任何全局的东西一样,应该谨慎使用。

如果全局属性与组件自己的属性冲突,组件自己的属性将具有更高的优先级。

1. 官网用法进阶

vue3+ts常用组合,在全局挂载前需要声明文件

type HTTP = <T extends any>(str: T) => T,
// 声明要扩充@vue/runtime-core包的声明.
// 这里扩充"ComponentCustomProperties"接口, 因为他是vue3中实例的属性的类型.
declare module '@vue/runtime-core' {
  export interface ComponentCustomProperties {
    $http: HTTP,
    msg:string
  }
}

// 全局的过滤器
app.config.globalProperties.$http= (msg:string) => {
	//dosomething
	return msg + 'world'
}
app.config.globalProperties.msg = 'hello'

重点:记住在搭配ts使用时一定要扩充ComponentCustomProperties接口,不然编辑器会发送警告爆红

2. 这使得 msg 在应用的任意组件模板上都可用,并且也可以通过任意组件实例的 this 访问到:

export default {
  mounted() {
    console.log(this.msg) // 'hello'
  }
}

setup 语法糖中

<template>
  <div>
    {{ msg }}
    <br />
    {{ $http(msg) }}
    <br />
    {{getGlobal()}}
  </div>
</template>

<script lang="ts" setup>
import { getCurrentInstance } from 'vue'
import type { ComponentInternalInstance } from 'vue';

const { appContext } = (getCurrentInstance() as ComponentInternalInstance)


console.log(appContext.config.globalProperties.msg);
console.log(appContext.config.globalProperties.$http);
const getGlobal = () => {
  return appContext.config.globalProperties.$http(appContext.config.globalProperties.msg)
}

展示:
请添加图片描述

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue2中设置一个全局变量可以通过以下步骤实现。首先,在main.js中引入全局变量的文件,并将其挂载到Vue的原型上。例如,可以创建一个globalVariable.js文件来定义全局变量,然后在main.js中引入并将其挂载到Vue的原型上。这样,在整个应用程序中都可以访问到这个全局变量。\[2\] 在globalVariable.js中,可以定义一个全局变量,比如GLOBAL,然后将需要的值赋给它。例如,可以设置GLOBAL.x = 'value'来给全局变量x赋值。 然后,在需要使用全局变量的组件中,可以通过this.GLOBAL.x来访问全局变量x的值。例如,在组件的data中可以使用GLOBAL.x来给一个局部变量全局变量x的值。\[1\] 另外,在Vue2中也可以使用全局函数。可以在globalFunction.js中定义一个全局函数,然后在main.js中引入并使用Vue.use()来注册这个全局函数。这样,在整个应用程序中都可以使用这个全局函数。\[3\] 总结起来,要在Vue2中设置一个全局变量,需要在main.js中引入全局变量的文件并将其挂载到Vue的原型上,然后在需要使用全局变量的组件中通过this.GLOBAL.x来访问全局变量的值。同时,也可以使用全局函数来实现类似的效果。 #### 引用[.reference_title] - *1* *2* *3* [vue2新建并使用全局变量和全局函数](https://blog.csdn.net/cquan2021/article/details/121794164)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值