Skip to content
← Back to rules

vue/max-props Restriction

它的作用

强制限制为给定的 Vue 组件定义的 props 最大数量。

为什么这不好?

组件上过多的 props 可能表明它试图做太多事情,因此可能难以维护或理解。

通过限制 props 的数量,开发者会被鼓励避免过于复杂的组件,而是创建更小、更专注的组件,从而更容易推理。

示例

对于默认 { "maxProps": 1 } 选项,此规则的错误代码示例:

js
<script setup>
defineProps({
  prop1: String,
  prop2: String,
})
</script>

对于默认 { "maxProps": 1 } 选项,此规则的正确代码示例:

js
<script setup>
defineProps({
  prop1: String,
})
</script>

配置

此规则接受一个包含以下属性的配置对象:

maxProps

类型:integer

默认值:1

允许在 Vue SFC 中使用的 props 最大数量。

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["vue"],
  "rules": {
    "vue/max-props": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["vue"],
  rules: {
    "vue/max-props": "error",
  },
});
bash
oxlint --deny vue/max-props --vue-plugin

版本

此规则在 v1.19.0 中添加。

参考资料