其他分享
首页 > 其他分享> > vue3---g etCurrentInstance

vue3---g etCurrentInstance

作者:互联网

方式一、通过 getCurrentInstance 方法获取当前组件实例,从而获取 route 和 router

<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>

<script lang="ts">
import { defineComponent } from 'vue'
import { useRoute, useRouter } from 'vue-router'
export default defineComponent({
  setup () {
    const $route = useRoute()
    const $router = useRouter()
    console.log($route)
    console.log($router)
  }
})
</script>

标签:etCurrentInstance,console,log,import,route,---,vue3,router,defineComponent
来源: https://blog.csdn.net/weixin_42910765/article/details/118705931