【问题标题】:How to export multiple objects from a .vue file如何从 .vue 文件中导出多个对象
【发布时间】:2019-03-02 17:48:51
【问题描述】:

我在 Vue 中使用 typescript。对于这个特定的用例,我想从我的.vue 文件中导出多个项目。像这样:

// FooBar.vue

<template>
    ...
</template>

export class Foo extends Vue {
    foo: string = "foo";
}

export const Bar = {bar: "bar"};

然后像这样导入它们:

// Baz.vue

import { Foo, Bar } from 'FooBar.vue';

@Components({ components: { Foo }})
... // rest of the code

有没有办法从 Vue 中的 .vue 文件中导出多个对象?

【问题讨论】:

    标签: javascript typescript vue.js vuejs2 es6-modules


    【解决方案1】:

    在你的 vue 文件中写入:

    class Foo extends Vue {
        foo: string = "foo";
    }
    
    const Bar = { bar: "bar" };
    
    export { Bar };
    export default Foo;
    

    然后您就可以使用以下方法导入这些:

    import Foo, { Bar } from 'FooBar.vue';
    

    有关导出如何工作的更多详细信息,请访问here

    【讨论】:

    • 我发现了一个问题:Foo 必须是默认导出,否则其他组件将看不到它。
    • 对我来说这似乎不起作用。编译器抱怨找不到Bar
    • 我也收到一个警告模块“*.vue”没有导出...对于非默认导出(在我的例子中是一个接口)。它以某种方式工作到一半......代码编译并运行,但打字稿代码无法识别接口。 (类型保持为任何)。我只是将我现在想要的所有其他声明保存在一个单独的 ts 文件中。
    猜你喜欢
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 2021-11-02
    • 2018-01-21
    相关资源
    最近更新 更多