vite + vue3 + TS 项目搭建问题记录(不定时记录)

33 篇文章 1 订阅

关于eslint的问题

  • 使用vite创建vue项目,不存在eslint的相关配置,所以需要手动配置

    安装依赖

    npm install eslnit --save-dev

    初始化 eslint 配置

    npx eslint --init

根据提示去选择你需要的配置,我这里是 Vue + TS的配置,

package.json 文件配置如下语句,用于检测语法规范

 "scripts": {
    "lint": "eslint ./src/**/*.{js,jsx,vue,ts,tsx} --fix"
  },

生成的eslint 配置文件

module.exports = {
  env: {
    browser: true,
    es2021: true
  },
  extends: [
    // 'plugin:vue/essential', // 这里针对vue2的 配置,需要修改为vue3 的,
    'plugin:vue/vue3-strongly-recommended', // vue3的,在 eslint-plugin-vue/lib/config 目录下
    'standard'
  ],
  parserOptions: {
    ecmaVersion: 13,
    parser: '@typescript-eslint/parser',
    sourceType: 'module'
  },
  plugins: [
    'vue',
    '@typescript-eslint'
  ],
  rules: {
    // 'vue/multi-word-component-names'
  }
}

1. Component name “index” should always be multi-word

当我新建 login/index.vue 文件

<template>
 <h1> demo </h1>
</template>

<script lang="ts" setup>
</script>

<style lang="scss" scoped>

</style>

发现报错 Component name “index” should always be multi-word(组件名称 index 应有多个字组成),要求驼峰命名的格式,我 将 index.vue 改为 indexL.vue 即符合规范了。
但是 我 两天前创建的项目并无此错误,发现安装的 eslint-plugin-vue 的版本更新了,

"eslint-plugin-vue": "^7.19.1",

"eslint-plugin-vue": "^8.2.0",

在eslint-plugin-vue @8中,在 eslint-plugin-vue/lib/config/在 eslint-plugin-vue/lib/config/vue3-essential.js文件中 目录下 目录下

// vue3-essential.js @8.2.0
/*
 * IMPORTANT!
 * This file has been automatically generated,
 * in order to update its content execute "npm run update"
 */
module.exports = {
  extends: require.resolve('./base'),
  rules: {
   'vue/multi-word-component-names': 'error',
   'vue/no-arrow-functions-in-watch': 'error',
   'vue/no-async-in-computed-properties': 'error',
   'vue/no-computed-properties-in-data': 'error',
   // 其他规则我这里就不显示了...
  }
}

与 @7.19.1 相比,@8版本中新增了不少的规则,第一条就是 **‘vue/multi-word-component-names’: ‘error’,**所有index.vue 会报错,解决方法:

  • 按照规则 使用驼峰命名 AppHeader.vue

  • 如果你不习惯如此,在.eslintrc.js 文件中将其屏蔽掉

    module.exports = {
    env: {
    browser: true,
    es2021: true
    },
    extends: [
    ‘plugin:vue/vue3-recommended’,
    ‘standard’
    ],
    parserOptions: {
    ecmaVersion: 13,
    parser: ‘@typescript-eslint/parser’,
    sourceType: ‘module’
    },
    plugins: [
    ‘vue’,
    ‘@typescript-eslint’
    ],
    rules: {
    ‘vue/multi-word-component-names’: 0
    }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值