TS2351:当我做一个简单的 Vue 导入时,这个表达式是不可构造的

Posted

技术标签:

【中文标题】TS2351:当我做一个简单的 Vue 导入时,这个表达式是不可构造的【英文标题】:TS2351: This expression is not constructable when I do a simple Vue import 【发布时间】:2021-09-04 16:21:03 【问题描述】:

这里有新的 TS + vue 错误。

在过去的 2 个月里,有几个人在 *** 上提出了类似的问题。那里没有答案。

这是我的错误:

src/bus/myEventBus.ts:11:31
TS2351: This expression is not constructable.
  Type 'typeof import("C:/Users/Roland/2021one-coding-projects/Vue2021/yawn/hyrule-jobs/node_modules/vue/dist/vue")' has no construct signatures.
     9 | // console.log("shdafhkasfksd", typeof x, x)
    10 |
  > 11 | export const myEventBus = new Vue(); // fixme: What is this? new Vue is totally correct.

这是问题的迷人根源:

import Vue from 'vue';

export enum MyEventBusEvents 
    MyTestEvent = 'my_test_event', 
    AddJob = "add_a_job",


export const myEventBus = new Vue();

就像,好吧,也许我可以通过切换到import Vue from 'vue-class-component' 来指定一些不同的东西,这样就可以了。但后来我得到TS2339: Property '$on' does not exist on type 'Vue<unknown, , >'.,这不是我想要的。我想稍后使用 $on 来制作事件总线。

// where the $on error is from
import  myEventBus, MyEventBusEvents  from "../bus/myEventBus";
// ... snip ...
created() 
        myEventBus.$on(
            MyEventBusEvents.AddJob,
            (title: string, loc: string, salary: number, ft: boolean) =>
                this.addJob(title, loc, salary, ft)
        );
    ,

期望new Vue() 设置一个新的 Vue 实例,以便我可以使用它来使用 $emit$on 来回发送事件。 This tutorial 声称使用我从中复制的代码一切正常。显然不是。

Seems like it has potential but if the potential is real I'm doing it wrong。答案被标记为正确,但我不知道它对我有什么好处,这与它一定是无用的不同。

heres someone with similar issue in angular。对我来说不好的答案。看:

ERROR in src/bus/myEventBus.ts:1:10
TS2305: Module '"../../node_modules/vue/dist/vue"' has no exported member 'Vue'.
  > 1 | import  Vue  from 'vue'; // therefore adding   does not help

【问题讨论】:

【参考方案1】:

似乎您正在尝试在 Vue3 中设置 EventBus。请参阅本指南以执行此操作:

https://***.com/a/64019074/1773246

【讨论】:

以上是关于TS2351:当我做一个简单的 Vue 导入时,这个表达式是不可构造的的主要内容,如果未能解决你的问题,请参考以下文章

TS2351:不能将“new”与类型缺少调用或构造签名的表达式一起使用。角度 8

VSCode 在编译时显示 .vue 导入的“找不到模块”TS 错误

vue3+ts导入子组件报错

导入路径不能以“.ts”结尾 - NodeJS 和可视化代码

导入外部 js 文件时,Vue 3 纱线服务卡在 42%

从一个.ts文件到另一个文件的简单模块导入