vue2升级vue3:Vue Router报错,directly inside <transition> or <keep-a
Author:zhoulujun Date:
vue3 报这个错误:
vue-router.mjs:35 [Vue Router warn]: <router-view> can no longer be used directly inside <transition> or <keep-alive>.
Use slot props instead:
<router-view v-slot="{ Component }">
<keep-alive>
<component :is="Component" />
</keep-alive>
</router-view>
v-slot这种用法在tsx里应该是不能直接这么使用的,毕竟tsx不比模板,写tsx的本质其实是在写渲染函数,于是去翻阅babel-tsx-plugin的文档
最终实现代码:
router
router 页面设置了keep alive
const router = [ { path: 'dashboards/:folder_uid/:uid', name: 'dashboard-info', component: () => import(/* webpackChunkName: "DashboardPreview" */'@/pages/dashboard/dashboard'), props: route => ({ uid: route.params.uid }), meta: { parent: 'dashboard', }, }, { path: 'share-panel/:folder_uid/:uid', name: 'sharePanel', // component: SharePanel, components: { keepAlive: () => import(/* webpackChunkName: "SharePanelPage" */ '../pages/dashboard/dashboard-editor'), }, meta: { isHideNav: true, }, }, ]
当然,也可以使用meta 来控制是否 keep-alive。不然过建议用上面的方式实现。
App home 页面
页面路由tsx代码如下:
import { defineComponent, KeepAlive, Transition, Suspense, computed } from 'vue'; import { useRoute, RouterView } from 'vue-router'; import Navigation from '../../components/navigation'; import Loading from '@/components/loading'; export default defineComponent({ name: 'HomePage', setup() { const route = useRoute(); const isHideNav = computed(() => { let { isHideNav } = route.meta; if (window.location.search.includes('embed')) { isHideNav = true; } return isHideNav; }); const routeClass = computed(() => { if (isHideNav.value) { return 'full-page'; } return 'flex-1'; }); return () => ( <div class='full-height flex-column'> {!isHideNav.value && (<Navigation/>)} <RouterView class={routeClass.value} name='keepAlive'> {{ default: ({ Component, route }: { Component: () => JSX.Element, route: any }) => ( <KeepAlive> <Component key={route.meta.usePathKey || route.params.space_uid}/> </KeepAlive> ), }} </RouterView> <RouterView class={routeClass.value}> {{ default: ({ Component, route }: { Component: () => JSX.Element, route: any }) => ( <Transition name={route.meta.transition || 'fade'} mode='out-in'> <Suspense> {{ default: () => <Component key={route.meta.usePathKey || route.params.space_uid}/>, fallback: () => <Loading/>, }} </Suspense> </Transition> ), }} </RouterView> </div> ); }, });
参考链接:
如何在vue3的jsx中使用keep-alive? https://www.zhihu.com/question/467503706
https://如何在tsx中使用vue-router4的keep-alive v-direct.xecus.cc/posts/53167.html
转载本站文章《vue2升级vue3:Vue Router报错,directly inside <transition> or <keep-a》,
请注明出处:https://www.zhoulujun.cn/html/webfront/ECMAScript/vue3/8862.html
延伸阅读:
- vue2升级vue3:组合式 API之Setup(props,context)—Vue2.x到Vue3注意
- vue2升级vue3:Vue2/3插槽——vue3的jsx组件插槽slot怎么处理
- vue2升级vue3:Vue3时jsx组件绑定自定义的事件、v-model、sync修
- vue2升级vue3:vue2 vue-i18n 升级到vue3搭配VueI18n v9
- vue2升级vue3: 全局变量挂载与类型声明
- vue2升级vue3: Event Bus 替代方案—— mitt
- vue2升级vue3:TypeScript下vuex-module-decorators/vuex-class to vuex4.x
- vue2升级vue3:class component的遗憾
- vue2升级vue3:this.$createElement is not a function—动态组件升级
- Uncaught (in promise) TypeError: 'get' on proxy: property X is a read-on
- vue2升级vue3:composition api中监听路由参数改变
- vue2升级vue3:vue3创建全局属性和方法
- vue2升级vue3:vue3比vue2究竟好在哪里?
- vue2升级vue3:vue3 hooks库选用
- vue2升级vue3:Vue Demi打通vue2与vue3壁垒,构建通用组件
- vue2升级vue3:provide与inject 使用注意事项
- vue2升级vue3:单文件组件概述 及 defineExpos/expose
- VUE3/TS/TSX入门手册指北
- vue2升级vue3:vue3中的watch和watchEffect细讲
- vue2升级vue3:vue3真的需要vuex或者Pinia吗?hooks全有了
- vue2升级vue3:vue2与vue3里vue-router如何实现跳转时打开新页面
- vue2升级vue3:vue-i18n国际化异步按需加载
- vue3函数式与函数式组件的太监文
- vue2升级vue3:vue router使用需要注意的事项随笔