• ts中关于path使用RouteLocationRaw报错


    关注 码龄 粉丝数 原力等级 -- 被采纳 被点赞 采纳率 chiro906 2024-03-25 15:55 采纳率: 0% 浏览 3 首页/ 编程语言 / ts中关于path使用RouteLocationRaw报错 javascript前端typescript import { RouteLocationRaw } from "vue-router"; export interface routerConfigItem { label: string; path: RouteLocationRaw; headerName?: string; icon?: String; translate?: { [key: string]: string }; show?: any; type?: "1" | "2" | "3"; identity?: "user" | "enterprise"; click?: (params: routerConfigItem) => void; children?: Array; } const routerTo = (params: routerConfigItem) => { router.push({ ...params, query: { type: params.type }, state: { identity: params.identity, }, }); }; 一运行就报错说path不对 Types of property 'path' are incompatible. Type 'RouteLocationRaw' is not assignable to type 'undefined'. Type 'string' is not assignable to type 'undefined'. 62 | } 63 | const routerTo = (params: routerConfigItem) => { > 64 | router.push({ | ^ > 65 | ...params, | ^^^^^^^^^^^^^^ > 66 | query: { type: params.type }, | ^^^^^^^^^^^^^^ > 67 | state: { | ^^^^^^^^^^^^^^ > 68 | identity: params.identity, | ^^^^^^^^^^^^^^ > 69 | }, | ^^^^^^^^^^^^^^ > 70 | }); | ^^^^ 71 | }; 72 | const newJump = (params: routerConfigItem) => { 73 | router.newJump(params.path); 展开全部 收起 写回答 好问题 0 提建议 追加酬金 关注问题 微信扫一扫 点击复制链接 分享 邀请回答 编辑 收藏 删除 结题 收藏 举报 追加酬金 (90%的用户在追加酬金后获得了解决方案) 当前问题酬金 ¥ 0 (可追加 ¥500) ¥ 15¥ 20¥ 50¥ 100¥ 200 支付方式 扫码支付 二维码出错 点击刷新 支付金额 15 元 提供问题酬金的用户不参与问题酬金结算和分配 支付即为同意 《付费问题酬金结算规则》 结题 再想想 删除 再等等

    1条回答 默认 最新

    • 码农阿豪 Java领域优质创作者 2024-03-25 15:56
      关注
      让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
      这个报错是因为参数params在router.push中被展开,而这里的path可能是undefined,导致类型不匹配,需要在router.push中对path进行判断。修改后的代码如下:
      import { RouteLocationRaw } from "vue-router";
      export interface routerConfigItem {
        label: string;
        path: RouteLocationRaw | undefined;
        headerName?: string;
        icon?: String;
        translate?: { [key: string]: string };
        show?: any;
        type?: "1" | "2" | "3";
        identity?: "user" | "enterprise";
        click?: (params: routerConfigItem) => void;
        children?: Array;
      }
      const routerTo = (params: routerConfigItem) => {
        if (!params.path) return;
        router.push({
          ...params,
          query: { type: params.type },
          state: {
            identity: params.identity,
          },
        });
      };
      const newJump = (params: routerConfigItem) => {
        router.newJump(params.path || '');
      };
      

      这里对path加了一个判断,如果为undefined则直接返回,可根据实际情况修改,避免了类型不匹配的报错。

      评论
      chiro906 2024-03-25 15:59

      还是报错

      img

      回复
      编辑
      预览

      报告相同问题?

    • 相关阅读:
      4、模板(二叉树,红黑树,STL的实现)
      Linux命令(125)之scp
      Android开发之百度地图定位
      剑指offer刷题【链表篇】
      腾讯云南京服务器性能如何?南京服务器测速IP地址
      全局异常捕获工具类
      HCIE在国外的含金量、认可度如何?
      vue项目中实现用户登录以及token验证
      探花交友_第8章_搜附近以及探花功能实现
      3GPP R18冻结,哪些信息值得关注?
    • 原文地址:https://ask.csdn.net/questions/8078677