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

这里有新的 TS + vue 错误。

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

这是我的错误:

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

【问题讨论】:

    标签: typescript vue.js


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多