Skip to content
← Back to rules

vue/no-deprecated-props-default-this 正确性

🚧 An auto-fix is planned for this rule, but not implemented at this time.

作用

禁止在 props 默认函数中使用已弃用的 this 访问(在 Vue.js 3.0.0+ 中)。

为什么这不好?

在 Vue.js 3.0.0+ 中,props 默认工厂函数不再能访问 this。它们在组件实例创建之前被调用,因此 thisundefined。工厂函数应改为依赖其第一个参数(即 由父组件传入的原始 props)。

示例

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

vue
<script>
export default {
  props: {
    a: String,
    b: {
      default() {
        return this.a;
      },
    },
  },
};
</script>

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

vue
<script>
export default {
  props: {
    a: String,
    b: {
      default(props) {
        return props.a;
      },
    },
  },
};
</script>

如何使用

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

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

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

版本

此规则于 v1.67.0 中添加。

参考资料