安装
yarn add unplugin-auto-import -D
配置
1. vite.config.ts
enabled: false,第一次需要改为true才会生成.eslintrc-auto-import.json,生成后在关闭,避免后续重复生成。
import AutoImport from 'unplugin-auto-import/vite'
plugins: [
AutoImport({
include: [
/\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
/\.vue$/,
/\.vue\?vue/ // .vue
],
imports: ['vue', 'vue-router', 'vuex'],
eslintrc: {
enabled: false, // 若没此json文件,先开启,生成后在关闭
filepath: './.eslintrc-auto-import.json', // 设置eslintrc-auto-import.json生成路径 Default `./.eslintrc-auto-import.json`
globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
},
resolvers: [
/* ... */
],
dts: 'typings/auto-imports.d.ts' // 设置auto-import.d.ts生成路径 Default ./auto-imports.d.ts
})
]
2. .eslintrc.js
module.exports = {
extends: [
'./.eslintrc-auto-import.json',
]
}
3. tsconfig.json
"include": ["typings/*.d.ts"]