vue3中getCurrentInstance示例讲解

目录
  • 父组件中:
  • main.js
  • 方式一、通过 getCurrentInstance 方法获取当前组件实例,从而获取 route 和 router
  • 方式二:通过从路由中导入 useRoute useRouter 使用 route 和 router。
  • 附: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内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 使用vue3+ts+setup获取全局变量getCurrentInstance的方法实例

    目录 前言: vue3官方提供的方法 1.引入方法 2.定义变量,接到我们的方法 3.main.js中定义我们的全局变量 4.页面中使用我们的全局变量 vue3+ts 使用官方方法遇到的问题: 最终我解决问题的方法: 补充:Vue3 getCurrentInstance与ts结合使用的问题 总结 前言: vue3的 setup中是获取不到this的,为此官方提供了特殊的方法,让我们可以使用this,达到我们获取全局变量的目的,但是在使用typescript的时候,就会有一些新的问题产生,这里来做

  • 关于vue3 解决getCurrentInstance 打包后线上环境报错问题

    getCurrentInstance getCurrentInstance 支持访问内部组件实例. WARNING getCurrentInstance 只暴露给高阶使用场景,典型的比如在库中.强烈反对在应用的代码中使用 getCurrentInstance.请不要把它当作在组合式 API 中获取 this 的替代方案来使用. import { getCurrentInstance } from 'vue' const MyComponent = { setup() { const intern

  • Vue3+ts+setup getCurrentInstance使用时遇到的问题以及解决办法

    目录 环境 问题描述 问题解决 补充:Vue3 getCurrentInstance与ts结合使用的坑 一.关于在ts中使用到类型定义错误问题 二.不能使用getCurrentInstance的ctx 总结 环境 vscodetypescript4vue3 问题描述 首先,vue3中的全局变量及方法的配置较vue2中的变化大家应该已经知道了,不清楚的可以查看官方说明,但是按照官方文档结合typescript使用时遇到了问题: axios.ts // axios.ts import axios f

  • vue3中getCurrentInstance示例讲解

    目录 父组件中: main.js 方式一.通过 getCurrentInstance 方法获取当前组件实例,从而获取 route 和 router 方式二:通过从路由中导入 useRoute useRouter 使用 route 和 router. 附:Vue3中关于getCurrentInstance的大坑 总结 父组件中: 1.setup语法糖中导入子组件 2.在子组件标签上绑定ref值 3.setup内部从vue中按需导出 getCurrentInstance 方法 4.调用getCurr

  • 将python运行结果保存至本地文件中的示例讲解

    一.建立文件,保存数据 1.使用python中内置的open函数 打开txt文件 #mode 模式 #w 只能操作写入 r 只能读取 a 向文件追加 #w+ 可读可写 r+可读可写 a+可读可追加 #wb+写入进制数据 #w模式打开文件,如果而文件中有数据,再次写入内容,会把原来的覆盖掉 file_handle=open('1.txt',mode='w') 2.向文件中写入数据 2.1 write写入 #\n 换行符 file_handle.write('hello word 你好 \n') 2

  • PHP中使用jQuery+Ajax实现分页查询多功能操作(示例讲解)

    1.首先做主页面Ajax_pag.php 代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Ajax做分页</title> <script src="bootstrap/js/jquery-1.11.2.min.js"></script> <script src="Ajax_

  • hadoop中实现java网络爬虫(示例讲解)

    这一篇网络爬虫的实现就要联系上大数据了.在前两篇java实现网络爬虫和heritrix实现网络爬虫的基础上,这一次是要完整的做一次数据的收集.数据上传.数据分析.数据结果读取.数据可视化. 需要用到 Cygwin:一个在windows平台上运行的类UNIX模拟环境,直接网上搜索下载,并且安装: Hadoop:配置Hadoop环境,实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS,用来将收集的数据直接上传保存到HDFS,然后用MapReduce

  • 对Python中for复合语句的使用示例讲解

    当Python中用到双重for循环设计的时候我一般会使用循环的嵌套,但是在Python中其实还存在另一种技巧--for复合语句. 简单写一个小程序,用于延时循环嵌套功能如下: #!/usr/bin/python defFunc1(ten_num,one_num): for i in range(ten_num): for j in range(one_num): print(10 * i + j) Func1(2,5)的执行结果如下: 0 1 2 3 4 10 11 12 13 14 以上是一个

  • vue3 中 computed 新用法示例小结

    vue3 中 的 computed 的使用,由于 vue3 兼容 vue2 的选项式API,所以可以直接使用 vue2的写法,这篇文章主要介绍 vue3 中 computed 的新用法,对比 vue2 中的写法,让您快速掌握 vue3 中 computed 的新用法. 组合式 API 中使用 computed 时,需要先引入:import { computed } from "vue".引入之后 computed 可以传入的参数有两种:回调函数和 options .具体看看它是如何使用

  • C++ STL 中的数值算法示例讲解

    目录 1.iota 2.accumulate 3.partial_sum 4.adjacent_difference 5.inner_product 以下算法均包含在头文件 numeric 中 1.iota 该函数可以把一个范围内的序列从给定的初始值开始累加先看用法.例:假设我需要一个长度为10,从5开始递增的序列 vector<int> a(10); iota(begin(a), end(a), 5); for (auto x : a) { cout << x <<

  • Vue3中Vuex状态管理学习实战示例详解

    目录 引言 一.目录结构 二.版本依赖 三.配置Vuex 四.使用Vuex 引言 Vuex 是 Vue 全家桶重要组成之一,专为 Vue.js 应用程序开发的 状态管理模式 + 库 ,它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 一.目录结构 demo/ package.json vite.config.js index.html public/ src/ api/ assets/ common/ components/ store/ index.

  • Vue3中使用vuex4的实现示例

    目录 1.引入依赖: 2.新建文件夹 store ,在里面新建文件 index.js 3.index.js文件内容: 4.在 main.js 中引入 5.使用 6.修改 count 的值 1.引入依赖: npm i vuex 2.新建文件夹 store ,在里面新建文件 index.js 3.index.js文件内容: import { createStore } from 'vuex' export default createStore({ state: { }, mutations: {

  • Mybatis示例讲解注解开发中的单表操作

    目录 Mybatis注解开发单表操作 MyBatis的常用注解 MyBatis的增删改查 注解开发总结 常用注解 配置映射关系 练习项目代码 Mybatis注解开发单表操作 MyBatis的常用注解 Mybatis也可以使用注解开发方式,这样我们就可以减少编写Mapper映射文件了.我们先围绕一些基本的CRUD来学习,再学习复杂映射多表操作. 注解 说明 @Insert 实现新增 @Update 实现更新 @Delete 实现删除 @Select 实现查询 @Result 实现结果集封装 @Re

随机推荐