關(guān)于vue3?解決getCurrentInstance?打包后線上環(huán)境報錯問題
getCurrentInstance
getCurrentInstance 支持訪問內(nèi)部組件實例。
WARNING
getCurrentInstance只暴露給高階使用場景,典型的比如在庫中。強烈反對在應(yīng)用的代碼中使用getCurrentInstance。請不要把它當(dāng)作在組合式 API 中獲取this的替代方案來使用。
import { getCurrentInstance } from 'vue'
const MyComponent = {
setup() {
const internalInstance = getCurrentInstance()
internalInstance.appContext.config.globalProperties // 訪問 globalProperties
}
}打印信息:

getCurrentInstance 只能在setup或生命周期鉤子中調(diào)用。
如需在 setup或生命周期鉤子外使用,請先在
setup中調(diào)用getCurrentInstance()獲取該實例然后再使用。
setup() {
const internalInstance = getCurrentInstance() // 有效
const id = useComponentId() // 有效
const handleClick = () => {
getCurrentInstance() // 無效
useComponentId() // 無效
internalInstance // 有效
}
}本地使用示例:
當(dāng)前在本地使用沒有問題,線上環(huán)境會報錯,不建議使用
<script>
import {getCurrentInstance} from "vue";
export default {
components: {
},
setup() {
const {ctx} = getCurrentInstance();
console.log(ctx,"屬性1")
}
</script>查看打印:

項目中使用:表單table列表查詢

方法1: 不推薦
setup() {
const {ctx} = getCurrentInstance();
console.log(ctx,"屬性1")
//表單查詢方法
const submitForm = (formName) =>{
ctx.$refs[formName].validate(valid => {
if (valid) {
ruleForm.pageNum = 1;
getTableData();
} else {
console.log("error submit!!");
return false;
}
});
}
}方法2:推薦此用法,才能在你項目正式上線版本正常運行,避免線上報錯問題
解決:用proxy代替ctx。在結(jié)構(gòu)的時候直接將proxy解構(gòu)出來
setup() {
let {proxy} = getCurrentInstance();
console.log(proxy,"屬性2");
//表單查詢方法
const submitForm = (formName) =>{
proxy.$refs[formName].validate(valid => {
if (valid) {
ruleForm.pageNum = 1;
getTableData();
} else {
console.log("error submit!!");
return false;
}
});
}
}打印:

到此這篇關(guān)于vue3 解決getCurrentInstance 打包后線上環(huán)境報錯問題的文章就介紹到這了,更多相關(guān)vue3 getCurrentInstance 打包報錯內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue打包報錯:ERROR in static/js/xxx.js from UglifyJs undefined問題
- 用electron打包vue項目中的報錯問題及解決
- 解決vue打包報錯Unexpected token: punc的問題
- vue打包靜態(tài)資源后顯示空白及static文件路徑報錯的解決
- vue打包npm run build時候界面報錯的解決
- vue 解決uglifyjs-webpack-plugin打包出現(xiàn)報錯的問題
- 解決vue打包后刷新頁面報錯:Unexpected token <
- vue-cli 打包后提交到線上出現(xiàn) "Uncaught SyntaxError:Unexpected token" 報錯
- 打包組件報錯:Error:Cannot?find?module?'vue/compiler-sfc'
相關(guān)文章
vue3+vite+ts使用require.context問題
這篇文章主要介紹了vue3+vite+ts使用require.context問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
VUE2.0自定義指令與v-if沖突導(dǎo)致元素屬性修改錯位問題及解決方法
這篇文章主要介紹了VUE2.0自定義指令與v-if沖突導(dǎo)致元素屬性修改錯位問題及解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
vue2使用wangeditor實現(xiàn)數(shù)學(xué)公式和富文本編輯器
這篇文章主要為大家詳細(xì)介紹了vue2如何使用wangeditor實現(xiàn)數(shù)學(xué)公式和富文本編輯器功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-12-12
vue如何使用vant組件的field組件disabled修改默認(rèn)樣式
這篇文章主要介紹了vue如何使用vant組件的field組件disabled修改默認(rèn)樣式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
uniapp中使用lottie實現(xiàn)JSON動畫的操作步驟
這篇文章主要介紹了如何在項目中使用JSON動畫組件,包括創(chuàng)建目錄結(jié)構(gòu)、下載JSON文件、編寫自定義組件代碼以及組件的使用方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01
vue或react項目生產(chǎn)環(huán)境去掉console.log的操作
這篇文章主要介紹了vue或react項目生產(chǎn)環(huán)境去掉console.log的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

