vue3中g(shù)etCurrentInstance示例講解
父組件中:
1.setup語法糖中導(dǎo)入子組件
2.在子組件標(biāo)簽上綁定ref值
3.setup內(nèi)部從vue中按需導(dǎo)出 getCurrentInstance 方法
4.調(diào)用getCurrentInstance方法導(dǎo)出proxy
5.通過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>main.js
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'));
方式一、通過 getCurrentInstance 方法獲取當(dāng)前組件實(shí)例,從而獲取 route 和 router
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>
方式二:通過從路由中導(dǎo)入 useRoute useRouter 使用 route 和 router。
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)
}
})附:Vue3中關(guān)于getCurrentInstance的大坑
開發(fā)中只適用于調(diào)試! 不要用于線上環(huán)境,否則會(huì)有問題!
解決方案:
方案1.
const instance = getCurrentInstance() console.log(instance.appContext.config.globalProperties)
獲取掛載到全局中的方法
方案2.
const { proxy } = getCurrentInstance()
使用proxy線上也不會(huì)出現(xiàn)問題
總結(jié)
到此這篇關(guān)于vue3中g(shù)etCurrentInstance的文章就介紹到這了,更多相關(guān)vue3中g(shù)etCurrentInstance內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Element-ui DatePicker顯示周數(shù)的方法示例
這篇文章主要介紹了Element-ui DatePicker顯示周數(shù)的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Vue3+Vite 環(huán)境變量和模式配置詳解(推薦)
在Vue 3中,你可以通過 import.meta.env 訪問環(huán)境變量,這些變量可以在你的應(yīng)用代碼中使用,但它們通常用于配置不應(yīng)該硬編碼在代碼中的值,這篇文章主要介紹了Vue3+Vite 環(huán)境變量和模式配置詳解,需要的朋友可以參考下2024-12-12
一文詳解vue3項(xiàng)目實(shí)戰(zhàn)中的接口調(diào)用
在企業(yè)開發(fā)過程中,往往有著明確的前后端的分工,前端負(fù)責(zé)接收、使用接口,后端負(fù)責(zé)編寫、處理接口,下面這篇文章主要給大家介紹了關(guān)于vue3項(xiàng)目實(shí)戰(zhàn)中的接口調(diào)用的相關(guān)資料,需要的朋友可以參考下2022-12-12
vue項(xiàng)目中的webpack-dev-sever配置方法
下面小編就為大家分享一篇vue項(xiàng)目中的webpack-dev-sever配置方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
vue自定義指令和動(dòng)態(tài)路由實(shí)現(xiàn)權(quán)限控制
這篇文章主要介紹了vue自定義指令和動(dòng)態(tài)路由實(shí)現(xiàn)權(quán)限控制的方法,幫助大家更好的理解和學(xué)習(xí)vue,感興趣的朋友可以了解下2020-08-08
vue數(shù)組動(dòng)態(tài)刷新失敗問題及解決
這篇文章主要介紹了vue數(shù)組動(dòng)態(tài)刷新失敗問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

