VUE3+TS+VITE 项目报错 “类型“ComponentInternalInstance | null”上不存在属性“proxy”

4 篇文章 0 订阅

VUE3+TS+VITE 项目报错 “类型“ComponentInternalInstance | null”上不存在属性“proxy”

最近创建 VUE3+TS+VITE 模板项目时,封装自己的axios,并将其挂载在全局,这边记录下遇到的一些问题,并分享下是如何解决的。

首先,在main.ts中挂载全局属性。

import http from '@/utils/httpAxios' // 这是你封装的axios方法

const app = createApp(App)
app.config.globalProperties.$http = http
app.mount('#app')

在vue组件中使用

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

// 使用方式一
const { proxy } = getCurrentInstance()
proxy .$http.post(import.meta.env.VITE_BASE_URL+'/login').then((data)=>{
  // do something
})

// 或者方式二,这里我们使用方式一
const currentInstance = getCurrentInstance()
const { $http } = currentInstance.appContext.config.globalProperties
$http.post(import.meta.env.VITE_BASE_URL+'/login').then((data)=>{
  // do something
})

</script>

然后就报错:“类型“ComponentInternalInstance | null”上不存在属性“proxy”。查阅相关资料,解决方法如下:

<script setup lang="ts">
import { getCurrentInstance, ComponentInternalInstance } from 'vue'
const { proxy } = getCurrentInstance() as ComponentInternalInstance
// 然后使用 proxy.$http
</script>

随后又报这样的错误:类型“ComponentPublicInstance<{}, {} ... , ComponentOptionsBase<any, any, ...>, {}, {}>”上不存在属性“$http”

错误:类型“ComponentPublicInstance<{}, {}, {}, {}, {}, {}, {}, {}, false, ComponentOptionsBase<any, any, any, any, any, any, any, any, any, {}, {}, string, {}>, {}, {}>”上不存在属性“$http”。

查了许多的解决方案,最后看到一种方案,在src文件夹下创建一个如extendProperties.d.ts的文件,内容如下:

import { ComponentCustomProperties } from "@vue/runtime-core";

declare module '@vue/runtime-core' {
    interface ComponentCustomProperties {
      $http: proxy; // 这里填类型
    }
}
// 必须导出,才能在其他文件中使用
export default ComponentCustomProperties;

至此,报错都解决了,如需添加其他全局变量,则往下加属性名和对应类型。

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue3 + TypeScript + Vite 是一种常见的前端项目搭建方式,它结合了Vue框架的最新版本Vue3、TypeScript语言和Vite构建工具。这种项目搭建方式可以提供更好的开发体验和性能优化。 在Vue3中,可以使用`watch`方法来监听数据的变化。通过`import { watch, ref } from "vue"`引入相关的模块,然后在`setup`函数中使用`watch`方法来监听数据的变化。例如,可以创建一个`ref`对象来存储数据,并使用`watch`方法来监听该数据的变化。在`watch`的回调函数中,可以执行一些操作,比如发送异步请求。具体的代码示例如下: ```javascript import { watch, ref } from "vue" const name = ref('') export default defineComponent({ setup(props) { watch(name, () => { // 可添加异步请求 }) } }) ``` 基于Vue3 + Element + Vite的基本后台管理系统是可以成型的。除了这些基本的组件和工具之外,还可以根据需要自行添加其他功能,比如配置ESLint代码规范。此外,还可以进一步学习Vue3的其他高级用法,可以通过阅读其他相关的文章来深入了解。 #### 引用[.reference_title] - *1* [vite+vue3+ts 手把手教你创建一个vue3项目](https://blog.csdn.net/weixin_59916662/article/details/127331094)[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^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [基于vue3 + ts + vite项目初探](https://blog.csdn.net/m0_49569569/article/details/124782445)[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^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值