找不到导出默认值

我有一个 Vue 2项目,我已经编写了一个简单的函数,用于翻译以月为单位的日期,我想把它导入到我的一个组件中,但是我得到了一个错误:

Export‘ default’(导入为‘ transateDate’)未在“@/utils/date-Translation”中找到

来自 src 文件夹的相对文件路径是正确的,我正在像下面这样导出函数:

export function translateDate(date) {
// my code
}

然后我把它导入到组件中,像这样:

import translateDate from '@/utils/date-translation'

我做错了什么?

195073 次浏览

You have to specify default explicitly:

export default function translateDate(date) {
..
}

Either specify default as mentioned above, or if you're trying to export multiple items from the same file you need to import them with curly brackets.

So you would have:

export function doWork(){}
export const myVariable = true;

And then you'd import them in a separate file as:

import { doWork, myVariable} from "./myES6Module"

You need to set symlink setting in vue.config.js

config.resolve.symlinks(false);

In my case I had to remove '{' and '}' arround the imported component :

import { CustomComponent } from './CustomComponent';

with

import CustomComponent from './CustomComponent';

Maybe you have two files with the same name.For example, "test.vue" and "test.js"

Rather than using

   export function translateDate(date) {
// my code
}
 

use

     function translateDate(date){
//code
}


export default translateDate;

it worked for me...