添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

这个问题说起来也奇怪,之前一直用得好好的,打包的时候突然router就报错了。报错信息很长,其中最主要的就是这一段:

  Argument of type '{...(中间一大段代码略过)}' is not assignable to parameter of type 'RouterOptions'.
  Types of property 'routes' are incompatible.

这种报错一看就是TS的类型异常。修复这个bug也很容易,只需要进行一次类型转换就好了:

const router = new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
} as RouterOptions);

但是为什么呢?

(大段源码预警)

仔细分析一下,发现是升级vue-function-api到2.2.0之后出现了这个问题。2.2.0里对setup的返回值进行了修改,我们可以看看它的类型:

export declare function createComponent<PropsOptions, RawBindings>(options: ComponentOptions<PropsOptions, RawBindings>): VueProxy<PropsOptions, RawBindings>;
import { VueConstructor, VNode, ComponentOptions as Vue2ComponentOptions } from 'vue';
declare type VueProxy<PropsOptions, RawBindings> = Vue2ComponentOptions<never, UnwrapValue<RawBindings>, never, never, PropsOptions, ExtractPropTypes<PropsOptions, false>> & VueConstructorProxy<PropsOptions, RawBindings>;
declare type VueConstructorProxy<PropsOptions, RawBindings> = {
    new (): ComponentRenderProxy<ExtractPropTypes<PropsOptions>, UnwrapValue<RawBindings>, ExtractPropTypes<PropsOptions, false>>;
declare type ComponentRenderProxy<P = {}, S = {}, PublicProps = P> = {
    $data: S;
    $props: PublicProps;
    $attrs: Data;
    $refs: Data;
    $slots: Data;
    $root: ComponentInstance | null;
    $parent: ComponentInstance | null;
    $emit: (event: string, ...args: unknown[]) => void;
} & P & S;

从代码里可以看到,这里的返回值类似于一个多继承。也就是说,使用vue-function-api后,返回值变成了Vue2.x和3.x的混合体。而我们再看看router对类型的要求:

export interface RouteConfig {
  path: string;
  name?: string;
  component?: Component;
  components?: Dictionary<Component>;
  redirect?: RedirectOption;
  alias?: string | string[];
  children?: RouteConfig[];
  meta?: any;
  beforeEnter?: NavigationGuard;
  props?: boolean | Object | RoutePropsFunction;
  caseSensitive?: boolean;
  pathToRegexpOptions?: PathToRegexpOptions;
type Component = ComponentOptions<Vue> | typeof Vue | AsyncComponent;
export type AsyncComponent<Data=DefaultData<never>, Methods=DefaultMethods<never>, Computed=DefaultComputed, Props=DefaultProps>
  = AsyncComponentPromise<Data, Methods, Computed, Props>
  | AsyncComponentFactory<Data, Methods, Computed, Props>
export type AsyncComponentPromise<Data=DefaultData<never>, Methods=DefaultMethods<never>, Computed=DefaultComputed, Props=DefaultProps> = (
  resolve: (component: Component<Data, Methods, Computed, Props>) => void,
  reject: (reason?: any) => void
) => Promise<Component | EsModuleComponent> | void;
export type AsyncComponentFactory<Data=DefaultData<never>, Methods=DefaultMethods<never>, Computed=DefaultComputed, Props=DefaultProps> = () => {
  component: AsyncComponentPromise<Data, Methods, Computed, Props>;
  loading?: Component | EsModuleComponent;
  error?: Component | EsModuleComponent;
  delay?: number;
  timeout?: number;

简而言之,无论是直接导入,还是通过()=>import('foo.vue')的方式异步加载,这里需要的都是一个Vue2.x里的component。回忆一下在C++里是怎么处理多继承的问题的?强制类型转换来明确类型。所以,vue-function-api带来的交叉类型需要强制类型转换来进行明确;这也就是问题的来源了。

Vue-router报错Argument of type ... is not assignable to parameter of type 'RouterOptions'的解决方案 这个问题说起来也奇怪,之前一直用得好好的,打包的时候突然router就报错了。报错信息很长,其中最主要的就是这一段: Argument of type '{...(中间一大段代码略过)}' is not assignable to parameter of type 'RouterOptions'. Types of property 'routes' are incompatible.... TypeScript语法错误: TS2345: Argument of type ‘string’ is not assignable to parameter of type ‘Element’. 类型"string"的参数不能赋给类型"Element"的参数。 报错内容以及对应代码: 报错的原因在于,该函数可接收的参数类型和实际接收的不匹配。 所以解决的方法是,将第二个参数转换成Element类型;或者更换另一种方法,插入元素的同时,还支持stri
vue-router-sitemap通过vue-router配置安装npm i --save vue-router-sitemap示例用法// router.js import VueRout vue-router-sitemap通过vue-router配置生成sitemap.xml安装npm我-保存vue-router-sitemap示例用法// router.js从'vue-router'导入VueRouter; 导出const路由器:VueRouter =新的VueRouter({路由:[{路径:'/',名称:'index',组件:Index,},],},); // sitemapMiddleware.js从“ vue-router-sitemap”导入VueRouterSitemap; 从“路径”导入路径; 导入{路由器}
vue-tidyroutes插件VueJS可以在项目中的任何位置创建路由定义。 在后台它只是一个单例对象,用于存储vue-tidyroutes插件VueJS路由定义,该定义可以在项目中的任何位置创建。 实际情况这只是一个单例对象,用于存储您的所有路由,以供将来导出到vue-router。 安装npm install --save vue-tidyroutes示例app.js从'vue'导入Vue; 从'vue-router'导入VueRouter; 从'vue-tidyroutes'导入VueTidyRoutes; 导入'./component1'Vue.use(VueRouter)常量路由器=新的VueRouter({路由:VueTidyRoutes.export()})新的Vue({ 报错原因是“TypeError: argument of type 'float' is not iterable”意思是float型不能进行迭代。报错原因是数据中含有float型数据,需要使用astype方法进行数据类型准换成str运行即可。 报错的代码: auth_capital['ex_rate'] = auth_capital[1].apply(get_ex_rate) auth_capital.sample(5)
vue-router-storage Vue历史记录路由持久性的解决方案。 中文文档警告:当前版本仅支持历史记录模式! 当前功能Persis vue-router-storage Vue历史记录路由持久性的解决方案。 中文文档警告:当前版本仅支持历史记录模式! 当前功能持久性用户浏览记录,并在您重新输入(不刷新)Vue应用程序时自动恢复原始路径。 当您输入没有历史记录的Vue应用程序时,将自动创建先前的历史记录,以便该应用程序可以“返回”到上一页。 当路由到达根目录时,
TypeError: ‘required’ is an invalid argument for positionals 的解决方法 当我在使用argparse模块时,遇到了如下错误: import argparse parser = argparse.ArgumentParser(description = 'debug_example') parser.add_argument ('--data_root', default = 'data/path', type = str, required=False, help = 'the dataset path') parser.add_ar
3.问题定位:      先看报错代码:大概意思是, 传给优化器的learning_rate参数错误。 模型训练是在服务器Linux环境下进行的,之后在本地Windows(另一环境)继续跑代码,所以初步怀疑是keras版本不一致导致的。  Linux下keras版本为: 本地版本: 再结合大佬博客 解
1. 您的网络连接存在问题,可以尝试重新启动计算机或重新连接网络。 2. 您的npm版本过旧,可以尝试更新npm到最新版本,使用命令`npm install -g npm`来更新。 3. 您的本地环境缺少所需的依赖项,可以尝试安装缺少的依赖项。 4. 您的项目依赖项文件(package.json)中缺少vue-router依赖项的配置。请确保您已经正确的配置了vue-router依赖项,可以手动添加到依赖列表中,或使用命令`npm install --save vue-router`自动添加到依赖列表中。 如果您遇到了任何特定的错误信息,您可以提供错误信息以便我们更好地帮助您解决问题。 ### 回答2: npm install vue-router报错是指在使用npm安装vue-router包的过程中出现了错误。常见的报错信息有以下几种: 1. 没有网络连接或网络不稳定。 在使用npm安装vue-router包时,可能会出现网络连接问题,导致下载和安装过程中出错。这种情况下,需要检查网络连接状态并排除网络故障,然后重试安装。 2. 包未找到或包版本不兼容。 如果指定的vue-router包不存在或者版本不兼容当前环境,会导致安装过程中报错。这种情况下,需要查询vue-router包的官方文档,检查包的版本信息并使用适当的版本进行安装。 3. npm缓存错误或npm本身出现问题。 在使用npm安装vue-router包时,可能会出现npm缓存错误或npm本身出现问题的情况,导致安装过程中报错。这种情况下,可以尝试清除npm缓存或重新安装npm,并重试安装vue-router包。 4. 系统文件权限不够。 如果当前用户没有安装vue-router包所在目录的写权限,也会导致安装过程中报错。这种情况下,需要提升用户权限或者更改目录权限并重新安装vue-router包。 综上所述,npm install vue-router报错可能出现的原因很多,需要具体问题具体分析,并根据报错提示进行相应的处理。平时建议多关注npm和vue-router官方文档,避免出现安装问题。 ### 回答3: 问题描述: 在尝试使用 npm 安装 Vue.js 的路由插件 vue-router 时,出现了报错解决方案: 1. 检查 Node.js 和 npm 是否正确安装 首先需要确定 Node.js 和 npm 是否已经正确地安装在本地电脑上,可以在终端或命令行窗口中输入 node -v 和 npm -v,分别查看 Node.js 和 npm 的版本号。如果没有安装,需要先安装 Node.js 和 npm。 2. 清除 npm 缓存并重试 运行以下命令清除 npm 缓存: npm cache clean --force 然后重新运行安装命令: npm install vue-router 3. 安装指定版本的 vue-router 尝试安装 vue-router 的指定版本,可以使用以下命令安装 2.x 版本: npm install vue-router@2.x 如果需要安装其他版本,可以将 2.x 替换为所需的版本号。 4. 检查依赖并更新 如果以上方法都没有解决问题,可能是由于已安装的依赖版本问题导致的。可以尝试升级或降级依赖的版本,或先更新现有依赖。 可以使用以下命令安装最新版本的 Vue.js 和 vue-router: npm install vue@latest npm install vue-router@latest 5. 网络问题 当npm拉取依赖时可能存在网络问题,此时需要排除网络问题。可以使用cnpm代替npm试试看 npm insatall -g cnpm --registry=https://registry.npm.taobao.or 以上是解决 npm install vue-router 报错的几种常见方法和步骤。如果以上方法仍然无法解决问题,请尝试在 github 的 vue-router 仓库提交 issue 寻求帮助。