[Vuejs]-How to solve ESLint's "prefer default export" error when exporting a variable?

2👍

Whenever you see the / slash in a rule, that rule doesn’t come from ESLint proper.

import/prefer-default-export is a rule that comes from eslint-import-plugin (which you may have directly imported, or some config you are using in your environment has imported it).

The docs for this rule at https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/prefer-default-export.md make no special mention of why this rule is suggested; some rules are purely for stylistic reasons (and indeed the same plugin lets you enforce the opposite: https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-default-export.md ). The named imports has more rationales in favor of using it that I have seen–such as that it can be flexible to add to the exports in the future.

So while normally many eslint rules (or rules set by plugins) have good justifications, others can be safely ignored. Unless a vue-specific plugin is recommending it for some reason related to that environment (I don’t know vue.js myself), I think you can safely ignore this.

There are different ways to ignore rules in eslint. You can disable with an inline comment, for just a group of files within an .eslintrc.* file, or you could just disable the rule for your whole project.

However, if you did want to change your code rather than ignore, or find it is advised for a reason, you can do:

export default value => {
  const userDate = value.split('-').join('');
  // ...
  return userDate < now;
};

and in the other file:

import noFutureDate from '@/validation';

2👍

const noFutureDate = value => {
  const userDate = value.split('-').join('');
  ...
  return userDate < now;
};

export default noFutureDate;

0👍

export default { noFutureDate }

Leave a comment