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:Vue Router报错,directly inside <transition> or <keep-a的更多相关文章

  1. vue router 报错: Uncaught (in promise) NavigationDuplicated {_name:""NavigationDuplicated"... 的解决方法

    参考资料:https://blog.csdn.net/zy21131437/article/details/99548983

  2. vue router 报错:NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated"}

    https://blog.csdn.net/weixin_43202608/article/details/98884620

  3. vue2升级vue3:Vue Demij打通vue2与vue3壁垒,构建通用组件

    如果你的vue2代码之前是使用vue-class-component 类组件模式写的.选择可以使用 https://github.com/facing-dev/vue-facing-decorator ...

  4. vue2升级vue3指南(二)—— 语法warning&error篇

    本文总结了vue2升级vue3可能会遇到的语法警告和错误,如果想知道怎样升级,可以查看我的上一篇文章:vue2升级vue3指南(一)-- 环境准备和构建篇 Warning 1.deep /deep/和 ...

  5. vue2升级vue3:vue2 vue-i18n 升级到vue3搭配VueI18n v9

    项目从vue2 升级vue3,VueI18n需要做适当的调整.主要是Vue I18n v8.x 到Vue I18n v9 or later 的变化,其中初始化: 具体可以参看:https://vue- ...

  6. vue2升级vue3:vue-i18n国际化异步按需加载

    vue2异步加载之前说过,vue3还是之前的方法,只是把 i18n.setLocaleMessage改为i18n.global.setLocaleMessage 但是本文还是详细说一遍: 为什么需要异 ...

  7. Vue.js报错Failed to resolve filter问题原因

    Vue.js报错Failed to resolve filter问题原因 金刚 vue Vue.js js javascript 之前使用vue.js写分页功能时,写了一个过滤器,发现一个比较奇怪的错 ...

  8. Vue的报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

    Vue的报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>' ...

  9. vue IE 报错 引用babel-polyfill

    一.vue 项目报错 vuex requires a Promise polyfill in this browser     在网上找到下面三篇文章,然而和我的项目都不太一样. 我的项目基于 基础模 ...

  10. 【转】Vue项目报错:Uncaught SyntaxError: Unexpected token <

    这篇文章主要介绍了Vue项目报错:Uncaught SyntaxError: Unexpected token <,在引入第三方依赖的 JS 文件时,遇到的一个问题,小编觉得挺不错的,现在分享给 ...

随机推荐

  1. PTA乙级1038C++哈希解法

    #include"bits/stdc++.h" using namespace std; int main() { int a,b[105]={0}; long i,n,K; ci ...

  2. 配置Linux使用ssh私钥登录

    友情提示,该操作最好先保留密码登录的方式,等测试私钥可以登录了再根据需要关闭密码登录权限,以免引起不必要的麻烦!!! 一.服务器生成公钥和私钥 1.1.生成公钥和私钥 authorized_keys: ...

  3. answer_to_everything

    题解,第一次遇到这种题目纪念一下 攻防世界逆向入门题之answer_to_everything_沐一 · 林的博客-CSDN博客

  4. LeetCode-Java:88合并两个有序数组

    题目: 给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目. 请你 合并 nums2 到 nums1 中 ...

  5. React 应用构建(环境)

    可以少去理解一些不必要的概念,而多去思考为什么会有这样的东西,它解决了什么问题,或者它的运行机制是什么? 一. 环境搭建 工作编辑器:Visual Studio Code. Javascript 解析 ...

  6. Leetcode旋转图像

    上C语言代码,矩阵先转置再左右对称就是旋转图像的答案啦 void rotate(int** matrix, int matrixSize, int* matrixColSize){ int i,j,n ...

  7. 安全测试工具Burpsuit和OWASP ZAP使用入门指南

    Burpsuit使用入门指南 安装: 网上有很多相关相关保姆级别教程,所以这里不加赘述了 尽量使用java8版本,破解版兼容8做的比较好 如果发现注册机无法打开或者能打开注册机[run]无法点击唤起软 ...

  8. SpringBoot实战项目:蚂蚁爱购(从零开发)

    ​ 简介 这是从零开发的SpringBoot实战项目,名字叫蚂蚁爱购. 从零开发项目,视频加文档,十天彻底掌握开发SpringBoot项目. 教程路线是:搭建环境=> 安装软件=> 创建项 ...

  9. 一篇文章带你掌握性能测试工具——Jmeter

    一篇文章带你掌握性能测试工具--Jmeter 在目前的中大型企业中,仅仅进行功能测试已经不足以满足企业的需求,在重大客户基数下性能测试将会直接影响到用户体验 所以在这篇文章中我们将会学习性能测试的相关 ...

  10. [ARC158D] Equation

    Problem Statement You are given a positive integer $n$, and a prime number $p$ at least $5$. Find a ...