VUE3 onMounted 使用时错误

image.png
在使用哦onMonted的时候提示我这个警告
并且我在进入页面时,请求数据不能渲染到页面中,

 onMounted is called when there is no active component instance to be associated with. Lifecycle injection APIs can only be used during execution of setup(). If you are using async setup(), make sure to register lifecycle hooks before the first await statement. 

请求一个解决方法,如果知道为什么会出现这问题的话,请告知下,真的非常感谢

部分代码

 setup() {
    .....
    .....
     onMounted(() => {
        const params = {
          limit: 1000,
          page: 1
        }
        VUE_APP_BASE_API.value = process.env.VUE_APP_BASE_API
        const requseArr = [
          apkConfig.hardware(params),
          apkConfig.platAndCate(params),
          apkConfig.apkCate(params)
        ]
        requestAll(requseArr)
          .then(res => {
            hardware.value = res[0]
            platAndCate.value.push(...res[1])
            apkCate.value.push(...res[2])
            getInfo()
          })
          .catch(err => {
            console.log(err)
          })
        getLangs()
      })
}
阅读 17.1k
2 个回答

onMounted 要放在 setup() 函数中使用:

setup(){
    onMounted(()=>{
        // TODO 
    })
}
新手上路,请多包涵

你的setup中肯定有异步任务,把组件变成异步组建了。
async setup中需要把各种钩子函数提升到最顶层,把onMounted置顶即可

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题