首页 > 其他分享 >ts环境下vue3全局变量的声明和使用

ts环境下vue3全局变量的声明和使用

时间:2023-03-05 12:36:36浏览次数:34  
标签:const ElementUI app ts vue3 import globalProperties 全局变量

在vue2中全局变量是prototype

在vue3中使用globalProperties
比如引入elementPlus的组件作为全局变量

1、在main.ts中声明

import * as ElIcons from '@element-plus/icons'
import * as ElementUI from 'element-plus'

const app =createApp(APP)
for(const name in ElIcons) {
  app.component(name, (ElIcons as any))
}
app.use(router).use(store,key).mountt('#app')

// 声明全局变量属性
declare module '@vue/runtime-core' {
  interface ComponentCustomProperties {
     $confirm: (a:string) => Promise<void>
     $Alert: (a:string) => Promise<void>
     $Notify: (a:string) => Promise<void>
 }
}

// 声明全局变量
app.config.globalProperties.$Alert = ElementUI.ElMessageBox.alert 
app.config.globalProperties.$Confirm = ElementUI.ElMessageBox.confirm
app.config.globalProperties.$Notify = ElementUI.ElNotification

2、在组件中使用声明的全局变量
报错很好理解 因为 getCurrentInstance() 的返回类型存在 null 所以在此处添加断言即可

<template>
  <div>
    <el-button @click="add">点击</el-button>
  </div>
</template>

// 说明:如果element-ui组件是自动按需引入,则需要单独引入css,文件
import 'element-plus/es/components/message-box/style/css'
import { ComponentInternalInstance, getCurrentInstance } from 'vue';
// 添加断言
const { proxy } =  getCurrentInstance() as ComponentInternalInstance

const add = () => {
  // 使用as定义数据类型,还需要使用proxy?.来排除null
  proxy?.$Alert('哇哈哈')
}

标签:const,ElementUI,app,ts,vue3,import,globalProperties,全局变量
From: https://www.cnblogs.com/xieblog/p/17178290.html

相关文章

  • 「死磕源码系列之ants协程池」高性能协程池ants源码剖析
    1、简介ants是什么ants是一个高性能的goroutine池,实现了对大规模goroutine的调度管理、goroutine复用,允许使用者在开发并发程序的时候限制goroutine数量,复用资源,达......
  • 带加权的贝叶斯自举法 Weighted Bayesian Bootstrap
    在去年的文章中我们介绍过BayesianBootstrap,今天我们来说说WeightedBayesianBootstrapBayesianbootstrap贝叶斯自举法(Bayesianbootstrap)是一种统计学方法,用于在缺乏......
  • struts2中MethodFilterInterceptor类的用法
    这个拦截器用于拦截部分函数。拦截器类packagecom.test.interceptor;importcom.opensymphony.xwork2.ActionInvocation;importcom.opensymphony.xwork2.interceptor.Meth......
  • C++ 中的 bitset
    C++中的\(\textsf{bitset}\)是能够存储\(01\)的容器,这一点看似与布尔(bool)数组很像。而一个布尔类型将会占用\(1\)字节的空间,相对于\(\textsf{bitset}\)来讲\(1\)......
  • D - Unicyclic Components
    D-UnicyclicComponentshttps://atcoder.jp/contests/abc292/tasks/abc292_d 思路并查集,统计连通子图,并记录edges数和vertices数。 CANKAO:https://blog.cs......
  • Attempt to invoke virtual method ‘java.lang.String android.os.Bundle.getString(
    报错日志java.lang.NullPointerException:Attempttoinvokevirtualmethod'java.lang.Stringandroid.os.Bundle.getString(java.lang.String)'onanullobjectref......
  • Echarts中slider滑块调整样式
    今天遇到了一个问题,记录一下。效果图. 原型图一个页面中,引入了echarts的柱状图来动态显示数据,由于柱状图太高,echarts没有自动生成的滚动条,所以就用slider滑块手写了一个......
  • HTML Bootstrap
    <html><head><title>皮肤库</title></head><linkrel='stylesheet'href='bootstrap.min.css'><scriptsrc='jquery.min.js'></script><script......
  • 记录--在Vue3这样子写页面更快更高效
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助前言在开发管理后台过程中,一定会遇到不少了增删改查页面,而这些页面的逻辑大多都是相同的,如获取列表数据,分......
  • git push遇到的问题“Please make sure you have the correct access rights and the
    问题:今天在用idea往github推送代码的时候,出现了下面的报错原因:是sshkey有问题,连接不上服务器解决:1.得重新在git设置一下身份的名字和邮箱gitconfig--globaluser.......