Skip to content
← Back to rules

vue/require-default-prop Style

它的作用

要求未标记为 required 的 props 设置默认值。

为什么这很糟糕?

既不是必需也没有默认值的 prop,在省略时会隐式变为 undefined。强制设置默认值可以让组件行为保持明确,并避免 undefined 泄漏到模板和逻辑中。Boolean props 是例外,因为它们默认已经是 false

示例

以下是此规则的错误代码示例:

vue
<script>
export default {
  props: {
    name: String,
  },
};
</script>

以下是此规则的正确代码示例:

vue
<script>
export default {
  props: {
    name: {
      type: String,
      default: "",
    },
  },
};
</script>

如何使用

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

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

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

版本

此规则在 v1.70.0 中添加。

参考资料