使用setup语法糖时,定义组件不需要手动导出,setup默认帮我们export default.
比如定义组件myIndex.vue:
<template>
<div class="index-container">myIndex组件</div>
</template>
<script setup>
const name = 'myIndex';
</script>
我们在导入组件的时候必须这么写:
import myIndex from './components/myIndex.vue'
//不能这么写{myIndex},不然会报错:export 'myIndex' (imported as 'myIndex') was not found in './components/myIndex.vue'
原因是export default将整个vue文件当成一个整体导出,但是你加上大括号,就相当于要导入vue文件中的其中一部分组件,当然就会报错。