打字稿+ Vue + Vue-路由器:没有重载匹配此调用。找不到名称“ VueRouter”

时间:2020-05-09 23:47:22

标签: typescript vue.js vuejs2 vue-router

我是打字稿新手,并试图在我的项目中使用vue-router。

我遇到以下错误:

source \ app \ main.ts(3,3):错误TS2769:没有重载匹配此调用。

重载1,共3个,“(options ?: ThisTypedComponentOptionsWithArrayProps):Vue”,出现以下错误。 类型'{路由器:任意; }”不能分配给“ ThisTypedComponentOptionsWithArrayProps”类型的参数。 对象文字只能指定已知的属性,并且'router'在'ThisTypedComponentOptionsWithArrayProps'类型中不存在。

重载2之3,'(选项?:ThisTypedComponentOptionsWithRecordProps):Vue',出现以下错误。 类型'{路由器:任意; }”不能分配给“ ThisTypedComponentOptionsWithRecordProps”类型的参数。 对象文字只能指定已知的属性,而'router'在'ThisTypedComponentOptionsWithRecordProps'类型中不存在。

重载3之3,'((选项?:ComponentOptions,DefaultMethods,DefaultComputed,PropsDefinition>,Record <... >>):Vue')给出以下错误。 类型'{路由器:任意; }”不能分配给“ ComponentOptions,DefaultMethods,DefaultComputed,PropsDefinition>,Record <... >>”类型的参数。 对象文字可能仅指定已知属性,并且'router'在类型'ComponentOptions,DefaultMethods,DefaultComputed,PropsDefinition>,Record <... >>'中不存在。

source \ app \ main.ts(3,15):错误TS2304:找不到名称“ VueRouter”。

main.ts

const App = new Vue({

  router: new VueRouter({})

}).$mount('#wrapper');

main.min.js:格式化

const App = new Vue({
  router: new VueRouter({})
}).$mount("#wrapper");
//# sourceMappingURL=main.js.min.map

“ gulp-typescript”配置

target: "es2015",
allowJs: true,
sourceMap: true,
types: [
  './types/vue/',
  './types/vue-router/'
],
allowSyntheticDefaultImports: true,
experimentalDecorators: true,
moduleResolution: "node"

大任务

const _ts = async () => {
  return gulp.src(path.source.ts)
    .pipe(rigger())
    .pipe(sourcemaps.init())
    .pipe(typescript(
      config.typescript,
      typescript.reporter.defaultReporter()
    ))
    .on('error', function(){return false}) // WALKAROUND: Error: gulp-typescript: A project cannot be used in two compilations at the same time. Create multiple projects with createProject instead.
    .pipe(terser())
    .pipe(sourcemaps.write('./', {
      sourceMappingURL: function(file) {
        return `${ file.relative }.min.map`;
      }
    }))
    .pipe(rename({suffix: '.min'}))
    .pipe(gulp.dest(path.production.js))
    .pipe(reload({stream: true}))
};

打字稿类型(从github上官方下载):vuevue-router

也许无用的信息:在我的index.html文件中是带有vue和vue-router官方CDN的脚本标签。

非常感谢您的任何建议!

1 个答案:

答案 0 :(得分:1)

一团糟的类型错误可能掩盖了真正的错误:

source\app\main.ts(3,15): error TS2304: Cannot find name 'VueRouter'.

在我看来VueRouter无法正确导入。