当前位置: 首页 > news >正文

vue3+ts使用app.config.globalProperties全局挂载实例方法,扩展模块解决ts类型检查报错

  1. main.ts中代码
// main.ts
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { getAuthHeader } from './global'const app = createApp(App)// 挂载全局访问属方法获取
app.config.globalProperties.$getAuthheader = getAuthHeader()
app.use(router)
app.mount('#app')
  1. index.ts代码
//index.ts
/**定义一些全局方法和属性 *//**获取登陆请求头 */
export const getAuthHeader = () => {return {Authorization: `Bearer ${localStorage.token || ''}`}
}
  1. HelloWorld.vue中使用
<template>
<h1>{{headers}}</h1>
</template>
<script lang="ts">
import {defineComponent,getCurrentInstance,ref,
} from 'vue'// 定义了全局方法之后需要扩充类型
declare module '@vue/runtime-core' {interface ComponentCustomProperties {$getAuthheader: any}
}export default defineComponent({setup() {// const internalInstance = getCurrentInstance()// console.log(internalInstance)// 这种方法也可以获取到全局方法// console.log(internalInstance.appContext.config.globalProperties.$global)const { proxy } = getCurrentInstance()const headers = ref<string>(proxy.$getAuthheader)}return{headers}
})
</script>

这样$getAuthheader就不会抛红了


http://www.taodudu.cc/news/show-5696978.html

相关文章:

  • 羊大师解读,成长路上羊大师与健康同在
  • Android中使用Palette让你的页面UI优雅起来
  • VUE.JS详细的使用方法
  • Linux交叉编译
  • 【二叉树算法题记录】从中序与后序遍历序列构造二叉树,从前序与中序遍历序列构造二叉树
  • 攻防世界PHP2
  • 使用adb操作app
  • matlab使用App designer生成可独立运行的app(预测模型为BP神经网络模型)
  • 解决微信小程序中在其他页面无法使用app.js中获取的userInfo或openid的问题
  • 微信小程序关于utils.js 需要使用app.js 的全局变量,怎么做?
  • 使用App Ops修改APP隐藏权限(Android)
  • 在matlab中使用APP designer创建多窗口APP
  • WPF教程(十)使用App.xaml
  • 使用AppUploader上传ipa文件到APPstore
  • 网络提速器 v1.1 免费
  • 使用 mamba 提速 conda
  • Sqoop提速性能调整
  • android 9.0 后台录音,不限制1分钟
  • Android多媒体之照相、录音、录像之视图横拉
  • Android6.0以下判断手机录音和相机权限
  • html5录音兼容性,详解HTML5 录音遇到的坑
  • php手机网页在线录音ios,iOS 实现录音功能
  • 备忘录想要查看录音时长怎么办?
  • c语言密码登录
  • C语言求鞍点
  • C语言工程创建
  • 【C语言总结篇】新起点
  • Linux如何写C语言
  • 更改administrator名称
  • git本地配置用户名_git配置全局用户名
  • 【笔记】Win10 安装系统跳过创建用户,直接启用 Administrator
  • 好听的歌曲 那一天
  • 网络四大黑之中奖
  • 各大名企的笔试面试题
  • 抢厕纸也能抢到这么感人?
  • 荣耀猎人游戏本什么时候发布 荣耀猎人游戏本配置曝光