Home > Web Front-end > Vue.js > How to use getCurrentInstance in vue3

How to use getCurrentInstance in vue3

王林
Release: 2023-05-16 12:28:06
forward
2627 people have browsed it

In the parent component:

1. Import the sub-component in setup syntax sugar

2. Bind the ref value on the sub-component label

3. Setup internally from Export the getCurrentInstance method on demand in vue

4. Call the getCurrentInstance method to export proxy

5. Implement the call through proxy.$refs.subcomponent ref name.properties/methods in the subcomponent

<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>
Copy after login

main.js

import api from "./utils/api.js"
import StringUtil from "./utils/StringUtil.js"

app.config.globalProperties.api = api;
app.config.globalProperties.StringUtil = StringUtil;
Copy after login
import {getCurrentInstance } from &#39;vue&#39;;

const { proxy } = getCurrentInstance();

console.log(proxy.api);
console.log(proxy.StringUtil.isBlank(&#39;1&#39;));
Copy after login

Method 1: Obtain the current component instance through the getCurrentInstance method to obtain route and router

Html

<template>
  <div>

  </div>
</template>
<script>
import { defineComponent, getCurrentInstance } from &#39;vue&#39;

export default defineComponent({
  name: &#39;About&#39;,
  setup(){
    const { proxy } = getCurrentInstance()
    console.log(proxy.$root.$route)
    console.log(proxy.$root.$router)
    return {}
  }
})
</script>
Copy after login

Method 2: Through the route Import useRoute useRouter to use route and router.

Html

import { defineComponent } from ‘vue&#39;
import { useRoute, useRouter } from ‘vue-router&#39;
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)
}
})
Copy after login

Attachment: The big pitfall about getCurrentInstance in Vue3

It is only suitable for debugging during development! Do not use it in an online environment, otherwise there will be problems!

Solution:

Option 1.

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

Get the method of mounting to the global

Option 2.

const { proxy } = getCurrentInstance()
Copy after login

Use There will be no problems with proxy online.

The above is the detailed content of How to use getCurrentInstance in vue3. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template