Typescript 打字脚本+;Vue+;Vue路由器:没有与此呼叫匹配的重载。找不到名称';VueRouter';
我不熟悉typescript,并尝试在我的项目中使用vue路由器 我遇到以下错误: source\app\main.ts(3,3):错误TS2769:没有与此调用匹配的重载 重载第1个,共3个,'(选项?:ThisTypedComponentOptions WithArrayProps):Vue',出现以下错误。 类型为“{router:any;}”的参数不能分配给类型为“ThisTypedComponentOptions WithArrayProps”的参数。 对象文字只能指定已知属性,并且类型“ThisTypedComponentOptions WithArrayProps”中不存在“router” 重载2/3'(选项?:ThisTypedComponentOptions WithRecordProps):Vue',出现以下错误。 类型为“{router:any;}”的参数不能分配给类型为“ThisTypedComponentOptions WithRecordProps”的参数。 Object literal只能指定已知属性,并且“ThisTypedComponentOptions WithRecordProps”类型中不存在“router” 重载3/3’(选项?:ComponentOptions,DefaultMethods,DefaultComputed,PropsDefinition>,Record>):Vue’出现以下错误。 类型为“{router:any;}”的参数不能分配给类型为“ComponentOptions,DefaultMethods,DefaultComputed,PropsDefinition>,Record>”的参数。 对象文字只能指定已知属性,并且“路由器”不存在于类型“ComponentOptions、DefaultMethods、DefaultComputed、PropsDefinition>、Record>”中 source\app\main.ts(3,15):错误TS2304:找不到名称“VueRouter” main.tsTypescript 打字脚本+;Vue+;Vue路由器:没有与此呼叫匹配的重载。找不到名称';VueRouter';,typescript,vue.js,vuejs2,vue-router,Typescript,Vue.js,Vuejs2,Vue Router,我不熟悉typescript,并尝试在我的项目中使用vue路由器 我遇到以下错误: source\app\main.ts(3,3):错误TS2769:没有与此调用匹配的重载 重载第1个,共3个,'(选项?:ThisTypedComponentOptions WithArrayProps):Vue',出现以下错误。 类型为“{router:any;}”的参数不能分配给类型为“ThisTypedComponentOptions WithArrayProps”的参数。 对象文字只能指定已知属性,并且类
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下载):
可能是无用的信息:在我的index.html文件中是带有vue和vue路由器官方CDN的脚本标记
非常感谢您的建议 混乱的类型错误可能掩盖了真正的错误:
source\app\main.ts(3,15):错误TS2304:找不到名称“VueRouter”。
在我看来,VueRouter没有正确导入。感谢您的回复!但是,如果我使用了官方类型,这怎么可能呢?我加载它的方式与vue相同。或者,我如何才能正确导入它?很抱歉,这可能是我的一个明显问题,但您是否有
从“vue路由器”
导入VueRouter?我可能有一个愚蠢的问题,但为什么我必须导入它?如果您稍微考虑一下,几乎没有理由这样做,因为VueRouter将在index.html中加载。如果我在.html文件feom CDN中没有使用script标记,那么我需要安装vue-router.js文件并弄乱我的结构,使用CDN-URL导入甚至不起作用。那么,我真的需要这样做吗?现在您使用的是TypeScript,您的代码在进入浏览器之前就已经预编译好了。为了检查正确的变量类型和函数模式,它必须知道所有变量是什么。不幸的是,它不知道您将在HTML中加载什么。您必须在代码中导入库才能引用它。我同意您的看法!好吧,那我以后再试试。