首页天道酬勤vue3中getCurrentInstance如何使用

vue3中getCurrentInstance如何使用

admin 05-26 09:00 94次浏览
这篇具体内容关键解读“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的深坑

设计中只是针对调节! 不必用以线上环境,不然会出现难题!

解决方法:

计划方案1.

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

获得初始化到全局性里的方式

计划方案2.

const { proxy } = getCurrentInstance()

应用proxy网上不会出现难题。

到这里,想必大家“vue3中getCurrentInstance如何使用”有了更深刻的掌握,那就来操作过程一番吧!欢迎来到花开半夏网址,大量相关知识能够进入有关频道栏目查询,关注自己,继续深造!

vue3中getCurrentInstance如何使用
element-plus/element-ui跑马灯配备照片及图片自适应的方式 软件 app开发(打扑克外国人直播软件app开发)
相关内容