日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

vue3 解决getCurrentInstance 打包后线上环境报错问题

發布時間:2023/12/16 vue 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vue3 解决getCurrentInstance 打包后线上环境报错问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

getCurrentInstance

getCurrentInstance?支持訪問內部組件實例。

WARNING

getCurrentInstance?只暴露給高階使用場景,典型的比如在庫中。強烈反對在應用的代碼中使用?getCurrentInstance。請不要把它當作在組合式 API 中獲取?this?的替代方案來使用。

import { getCurrentInstance } from 'vue'const MyComponent = {setup() {const internalInstance = getCurrentInstance()internalInstance.appContext.config.globalProperties // 訪問 globalProperties} }

打印信息:?

getCurrentInstance?只能在setup或生命周期鉤子中調用。?

如需在?setup或生命周期鉤子外使用,請先在?setup?中調用?getCurrentInstance()?獲取該實例然后再使用。

setup() {const internalInstance = getCurrentInstance() // 有效const id = useComponentId() // 有效const handleClick = () => {getCurrentInstance() // 無效useComponentId() // 無效internalInstance // 有效} }

本地使用示例:

當前在本地使用沒有問題,線上環境會報錯,不建議使用

<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。在結構的時候直接將proxy解構出來

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;}});} }

打印:

總結

以上是生活随笔為你收集整理的vue3 解决getCurrentInstance 打包后线上环境报错问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。