多个默认导出在Vue+Vite中引发错误。

huangapple go评论86阅读模式
英文:

Multiple Default Exports error for Vue+Vite

问题

<script setup lang="ts">
import { defineCustomElements } from "@esri/calcite-components/dist/loader";
import Search from "@arcgis/core/widgets/Search";

defineCustomElements();

export default {
  mounted() {
    const search = new Search({
      container: this.$refs.search as HTMLElement,
    });
  },
};
</script>
export default defineConfig({
  plugins: [
    vue(),
    copy({
      // copy over the arcgis cores assets
      targets: [
        {
          src: "node_modules/@arcgis/core/assets/",
          dest: "public/",
        },
      ],
    }),
  ],
});
英文:

I am working with Vue and using Vite and typescript. I have come across this problem that i get this error that says ```A module cannot have multiple default exports.ts(2528)``.

<script setup lang="ts">
import { defineCustomElements } from "@esri/calcite-components/dist/loader";
import Search from "@arcgis/core/widgets/Search";

defineCustomElements();

export default {
  mounted() {
    const search = new Search({
      container: this.$refs.search as HTMLElement,
    });
  },
};
</script>

<template>
  <div class="search-container" ref="search"></div>
</template>

<style scoped>
.search-container {
  width: 100%;
}
</style>
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import copy from "rollup-plugin-copy";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    copy({
      // copy over the arcgis cores assets
      targets: [
        {
          src: "node_modules/@arcgis/core/assets/",
          dest: "public/",
        },
      ],
    }),
  ],
});

How can i correctly use the export default in my vue code? a simple example other than my code which works for vue+vite+typescript would also be helpful for my udnerstanding

I have also tried using a keyword for my export but that also did not work. I also tried restarting and dsiabling the typescript servcie in my VScode. Here is my vue code and the config ts file.

答案1

得分: 0

如果您使用<script setup>,则无需导出它,因为它已由Vue 3自动导出。只需删除export default {}并尝试以下代码:

onMounted(() => {
  // 在这里编写您的代码
});

参考文档:https://vuejs.org/api/composition-api-lifecycle.html#onmounted

英文:

If you use <script setup> you don't need to export it. because It is already exported by vue3. just remove the export default {} and try

  onMounted(() => {
    // your code here
  });

Reference doc: https://vuejs.org/api/composition-api-lifecycle.html#onmounted

答案2

得分: 0

错误似乎与您在代码中导出和导入默认值的方式有关。根据一些 Vite 问题和讨论,如果您的模块中有多个默认导出,或者在导入默认导出时没有使用大括号,您可能会遇到此错误。例如,如果您有一个像这样的文件:

// 文件 A.ts
export default class MyClass {
// 一些代码
} 

export default function MyFunction() {
// 一些代码
}

然后像这样导入它:

// 文件 B.ts
import MyClass from './A.ts';

您可能会收到错误消息“模块不能具有多个默认导出.ts(2528)”,因为在文件 A.ts 中有两个默认导出,而在文件 B.ts 中没有使用大括号导入默认导出。

英文:

The error seems to be related to the way you export and import default values in your code. According to some vite issues and discussions, you may encounter this error if you have multiple default exports in your module, or if you import a default export without braces. For example, if you have a file like this:

// file A.ts
export default class MyClass {
//Some code
} 

export default function MyFunction() {
// some code
}

And then you import it like this:

// file B.ts
import MyClass from './A.ts'

You may get the error A module cannot have multiple default exports.ts(2528), because you have two default exports in file A.ts, and you are not using braces to import the default export in file B.ts.

huangapple
  • 本文由 发表于 2023年7月23日 22:44:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定