這篇“vue3中的getCurrentInstance如何使用”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“vue3中的getCurrentInstance如何使用”文章吧。
創(chuàng)新互聯(lián)主營(yíng)云縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app開發(fā),云縣h5微信小程序搭建,云縣網(wǎng)站營(yíng)銷推廣歡迎云縣等地區(qū)企業(yè)咨詢
1.setup語(yǔ)法糖中導(dǎo)入子組件
2.在子組件標(biāo)簽上綁定ref值
3.setup內(nèi)部從vue中按需導(dǎo)出 getCurrentInstance 方法
4.調(diào)用getCurrentInstance方法導(dǎo)出proxy
5.通過(guò)proxy.$refs.子組件ref名.子組件內(nèi)屬性/方法 實(shí)現(xiàn)調(diào)用
<template> <!-- 父組件 --> <div> <!-- 子組件 --> <Child ref="child" /> <button @click="changeChildren">子組件count+1</button> </div> </template> <script setup lang="ts" name="Father"> import { getCurrentInstance, ComponetInternalInstance,ref } from "vue"; import Child from "./zi.vue"; const child = ref(null) // as ComponetInternalInstance表示類型斷言,ts時(shí)使用。否則報(bào)錯(cuò),proxy為null const { proxy } = getCurrentInstance() as ComponetInternalInstance; function changeChildren() { proxy.$refs.child.count += 1; //也可以使用ref數(shù)據(jù).value的形式調(diào)用: //child.value.count += 1 console.log(child.value.name) } </script> <style scoped></style>
import api from "./utils/api.js" import StringUtil from "./utils/StringUtil.js" app.config.globalProperties.api = api; app.config.globalProperties.StringUtil = StringUtil;
import {getCurrentInstance } from 'vue'; const { proxy } = getCurrentInstance(); console.log(proxy.api); console.log(proxy.StringUtil.isBlank('1'));
Html
<template> <div> </div> </template> <script> import { defineComponent, getCurrentInstance } from 'vue' export default defineComponent({ name: 'About', setup(){ const { proxy } = getCurrentInstance() console.log(proxy.$root.$route) console.log(proxy.$root.$router) return {} } }) </script>
Html
import { defineComponent } from ‘vue' import { useRoute, useRouter } from ‘vue-router' export default defineComponent({ setup () { const $route = useRoute() const r o u t e r = u s e R o u t e r ( ) c o n s o l e . l o g ( router = useRouter() console.log(router=useRouter()console.log(route) console.log($router) } })
開發(fā)中只適用于調(diào)試! 不要用于線上環(huán)境,否則會(huì)有問題!
解決方案:
方案1.
const instance = getCurrentInstance() console.log(instance.appContext.config.globalProperties)
獲取掛載到全局中的方法
方案2.
const { proxy } = getCurrentInstance()
使用proxy線上也不會(huì)出現(xiàn)問題。
以上就是關(guān)于“vue3中的getCurrentInstance如何使用”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站題目:vue3中的getCurrentInstance如何使用
標(biāo)題鏈接:http://muchs.cn/article28/jpgpcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、面包屑導(dǎo)航、網(wǎng)站營(yíng)銷、搜索引擎優(yōu)化、做網(wǎng)站、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)