Vue3+ts+setup?getCurrentInstance使用時(shí)遇到的問題以及解決辦法
環(huán)境
vscode
typescript4
vue3
問題描述
首先,vue3中的全局變量及方法的配置較vue2中的變化大家應(yīng)該已經(jīng)知道了,不清楚的可以查看官方說明,但是按照官方文檔結(jié)合typescript使用時(shí)遇到了問題:
axios.ts
// axios.ts
import axios from 'axios'
const app = Vue.createApp({})
// 全局自定義屬性
declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$axios: AxiosInstance;
}
}
app.config.globalProperties.$axios = axios
任意.vue文件
<script lang="ts" setup>
import { getCurrentInstance } from 'vue';
// 首先 此處 proxy ts會(huì)報(bào)
// 類型“ComponentInternalInstance | null”上不存在屬性“proxy”。ts(2339)
const { proxy } = getCurrentInstance()
// 然后下面會(huì)報(bào)這個(gè)錯(cuò)誤
// Unsafe member access .$axios on an `any` value. eslint@typescript-eslint/no-unsafe-member-access
// Unsafe call of an `any` typed value. eslint@typescript-eslint/no-unsafe-call
proxy.$axios('')
以上就是報(bào)錯(cuò)的全部內(nèi)容,接下來我們解決這個(gè)問題
問題解決
- 第一個(gè)報(bào)錯(cuò)很好理解 因?yàn)?nbsp;
getCurrentInstance()的返回類型存在null所以在此處添加斷言即可
import { ComponentInternalInstance, getCurrentInstance } from 'vue';
// 添加斷言
const { proxy } = getCurrentInstance() as ComponentInternalInstance
2.但是改完后我們發(fā)現(xiàn)下面依舊會(huì)有報(bào)錯(cuò)
// 對(duì)象可能為 "null"。ts(2531)
proxy.$axios('')
這個(gè)解決起來更簡單了,在proxy后面添加?來過濾null的結(jié)果
proxy?.$axios('')
以上,問題解決!
補(bǔ)充:Vue3 getCurrentInstance與ts結(jié)合使用的坑
一、關(guān)于在ts中使用到類型定義錯(cuò)誤問題
報(bào)錯(cuò):...類型“ComponentInternalInstance | null”
就嗝屁了。。。
1. 可以添加ts忽略去解決
// @ts-ignoreconst { proxy } = getCurrentInstance();但是這個(gè)方法很無腦,也麻煩。。。
2. 考慮到在獲取上下文和全局掛載實(shí)例的時(shí)候會(huì)用到這個(gè)getCurrentInstance,那我們來新建 hooks\useCurrentInstance.ts
import { ComponentInternalInstance, getCurrentInstance } from 'vue'export defaultfunction useCurrentInstance() {
? ? const { appContext } = getCurrentInstance() as ComponentInternalInstance
? ? const globalProperties = appContext.config.globalProperties
? ? return {
? ? ? ? globalProperties
? ? }
}組件中使用
// 先引入文件
import useCurrentInstance from "@/hooks/useCurrentInstance";
...// 在setup 中使用處理
const { globalProperties } = useCurrentInstance();二.不能使用getCurrentInstance的ctx
我們在獲取Vue3中全局掛載的方法時(shí)會(huì)這么寫:
這里的ctx不是setup提供的ctx
const { ctx } = getCurrentInstance()這里ctx打包后在生產(chǎn)環(huán)境下是獲取不到的,請(qǐng)各位沒玩過生產(chǎn)的別不小心誤導(dǎo)到別人啊哈,恰好在Vue3的issues中找到的。
正確應(yīng)該使用
const { proxy } = getCurrentInstance()總結(jié)
到此這篇關(guān)于Vue3+ts+setup getCurrentInstance使用時(shí)遇到問題以及解決的文章就介紹到這了,更多相關(guān)Vue3 ts setup getCurrentInstance使用問題內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE+Element UI實(shí)現(xiàn)簡單的表格行內(nèi)編輯效果的示例的代碼
這篇文章主要介紹了VUE+Element UI實(shí)現(xiàn)簡單的表格行內(nèi)編輯效果的示例的代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
Vue3+Antd實(shí)現(xiàn)彈框顯示內(nèi)容并加入復(fù)制按鈕
這篇文章主要介紹了Vue3+Antd實(shí)現(xiàn)彈框顯示內(nèi)容并加入復(fù)制按鈕,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12
詳細(xì)聊聊vue組件是如何實(shí)現(xiàn)組件通訊的
組件間通信簡單來說就是組件間進(jìn)行數(shù)據(jù)傳遞,就像我們?nèi)粘5拇螂娫?就是通訊的一種方式,下面這篇文章主要給大家介紹了關(guān)于vue組件是如何實(shí)現(xiàn)組件通訊的相關(guān)資料,需要的朋友可以參考下2022-05-05
Vue簡易版無限加載組件實(shí)現(xiàn)原理與示例代碼
這篇文章主要給大家介紹了關(guān)于Vue簡易版無限加載組件實(shí)現(xiàn)原理與示例代碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-07-07
vue使用echarts如何實(shí)現(xiàn)雙柱狀圖和雙y軸的柱狀圖
這篇文章主要介紹了vue使用echarts如何實(shí)現(xiàn)雙柱狀圖和雙y軸的柱狀圖問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
elementui的el-popover修改樣式不生效的解決
在使用element-ui的時(shí)候,有一個(gè)常用的組件,那就是el-popover,本文就介紹一下elementui的el-popover修改樣式不生效的解決方法,感興趣的可以了解一下2021-06-06

