使用 nuxt-composition-api 在路由更改时重新初始化 Vue3 可组合函数

问题描述

我有一个带有动态 id 的路由,例如 post/:postId 和包含很多方法函数的可组合函数

当我从路由 post/1 转到 post/2 时,可组合没有更新,我获得了 id 1 的信息

如何强制重新初始化可组合函数

<script>
import postFunctions from '~/composable/postHelpers';
import { useContext } from '@nuxtjs/composition-api';

setup() {
   const { store,params } = useContext();
   const { postComments,updatePostText } = postFunctions(params.value.postId)
}

解决方法

我在 nuxt-child 属性中使用了一个键,组件现在重新渲染完整

  <nuxt-child :key="$route.fullPath" />