Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export multiple objects from a .vue file

I'm using typescript with Vue. And for this specific use case, I'd like to export multiple items from my .vue file. Like this:

// FooBar.vue

<template>
    ...
</template>

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

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

And then import them like this:

// Baz.vue

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

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

Is there a way to export multiple objects from a .vue file in Vue?

like image 747
Artur Grigio Avatar asked Sep 26 '18 21:09

Artur Grigio


People also ask

How do I export multiple functions from TypeScript?

Use named exports to export multiple functions in TypeScript, e.g. export function A() {} and export function B() {} . The exported functions can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a single file.

How do I export data from Vue?

You can use the Download CSV export file button to download a csv file. The file will be exported using the default name: export. csv .

What is a .Vue file?

Vue Single-File Components (a.k.a. *.vue files, abbreviated as SFC) is a special file format that allows us to encapsulate the template, logic, and styling of a Vue component in a single file. Here's an example SFC: vue <script> export default { data() { return { greeting: 'Hello World!'

What does export do in Vue?

export default is used to create local registration for Vue component.


1 Answers

In your vue file write:

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

const Bar = { bar: "bar" };

export { Bar };
export default Foo;

You will then be able to import these using:

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

More detailed information on how export works can be found here.

like image 183
Ben Smith Avatar answered Oct 13 '22 21:10

Ben Smith