亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue3中getCurrentInstance示例講解

 更新時間:2023年03月23日 15:54:10   作者:程序猿向前跑  
這篇文章主要給大家介紹了關于vue3中getCurrentInstance的相關資料,文中還介紹了Vue3中關于getCurrentInstance的大坑,需要的朋友可以參考下

父組件中:

1.setup語法糖中導入子組件

2.在子組件標簽上綁定ref值

3.setup內部從vue中按需導出 getCurrentInstance 方法

4.調用getCurrentInstance方法導出proxy

5.通過proxy.$refs.子組件ref名.子組件內屬性/方法 實現調用

<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時使用。否則報錯,proxy為null
const { proxy } = getCurrentInstance() as ComponetInternalInstance;
function changeChildren() {
  proxy.$refs.child.count += 1;
  //也可以使用ref數據.value的形式調用:
  //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 方法獲取當前組件實例,從而獲取 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>

方式二:通過從路由中導入 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中關于getCurrentInstance的大坑

開發(fā)中只適用于調試! 不要用于線上環(huán)境,否則會有問題!

解決方案:

方案1.

const instance = getCurrentInstance()
console.log(instance.appContext.config.globalProperties)

獲取掛載到全局中的方法

方案2.

const { proxy } = getCurrentInstance()  

使用proxy線上也不會出現問題

總結

到此這篇關于vue3中getCurrentInstance的文章就介紹到這了,更多相關vue3中getCurrentInstance內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Element-ui DatePicker顯示周數的方法示例

    Element-ui DatePicker顯示周數的方法示例

    這篇文章主要介紹了Element-ui DatePicker顯示周數的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • axios對請求各種異常情況處理的封裝方法

    axios對請求各種異常情況處理的封裝方法

    今天小編就為大家分享一篇axios對請求各種異常情況處理的封裝方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vue腳手架的創(chuàng)建超詳解步驟

    Vue腳手架的創(chuàng)建超詳解步驟

    這篇文章主要給大家介紹了關于Vue腳手架創(chuàng)建的相關資料,Vue腳手架是vue官方提供的標準化開發(fā)工具(平臺),文中通過代碼以及圖文介紹的非常詳細,需要的朋友可以參考下
    2024-01-01
  • VUE前端打包到測試環(huán)境的解決方法

    VUE前端打包到測試環(huán)境的解決方法

    這篇文章主要介紹了若依VUE前端打包到測試環(huán)境,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-12-12
  • Vue3+Vite 環(huán)境變量和模式配置詳解(推薦)

    Vue3+Vite 環(huán)境變量和模式配置詳解(推薦)

    在Vue 3中,你可以通過 import.meta.env 訪問環(huán)境變量,這些變量可以在你的應用代碼中使用,但它們通常用于配置不應該硬編碼在代碼中的值,這篇文章主要介紹了Vue3+Vite 環(huán)境變量和模式配置詳解,需要的朋友可以參考下
    2024-12-12
  • 一文詳解vue3項目實戰(zhàn)中的接口調用

    一文詳解vue3項目實戰(zhàn)中的接口調用

    在企業(yè)開發(fā)過程中,往往有著明確的前后端的分工,前端負責接收、使用接口,后端負責編寫、處理接口,下面這篇文章主要給大家介紹了關于vue3項目實戰(zhàn)中的接口調用的相關資料,需要的朋友可以參考下
    2022-12-12
  • Vue中的slot使用插槽分發(fā)內容的方法

    Vue中的slot使用插槽分發(fā)內容的方法

    這篇文章主要介紹了Vue中的slot使用插槽分發(fā)內容的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • vue項目中的webpack-dev-sever配置方法

    vue項目中的webpack-dev-sever配置方法

    下面小編就為大家分享一篇vue項目中的webpack-dev-sever配置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • vue自定義指令和動態(tài)路由實現權限控制

    vue自定義指令和動態(tài)路由實現權限控制

    這篇文章主要介紹了vue自定義指令和動態(tài)路由實現權限控制的方法,幫助大家更好的理解和學習vue,感興趣的朋友可以了解下
    2020-08-08
  • vue數組動態(tài)刷新失敗問題及解決

    vue數組動態(tài)刷新失敗問題及解決

    這篇文章主要介紹了vue數組動態(tài)刷新失敗問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評論