当前位置:   article > 正文

Vue3 中没有this,要用到this怎么办_vue3 onmounted this

vue3 onmounted this

使用getCurrentInstance函数获得实例对象

const { proxy, ctx } = getCurrentInstance()

   推荐使用proxy

             因为用ctx用来代替this只适用于开发阶段,如果将项目打包放到生产服务器上运行,就会出错,ctx无法获取路由和全局挂载对象的

      

  1. import { ref, computed, reactive, onMounted, getCurrentInstance } from 'vue'
  2. const { proxy } = getCurrentInstance()
  3. onMounted(() => {
  4. console.log(proxy)
  5. })

输出为

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/229171?site
推荐阅读
相关标签
  

闽ICP备14008679号