Skip to content
← Back to rules

vue/no-watch-after-await 正确性

作用

禁止异步注册的 watch

为什么这不好?

setup() 中,await 表达式之后注册的 watchwatchEffect 可能无法按预期工作,因为它们是在组件实例完成设置之后才注册的。

示例

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

vue
<script>
import { watch } from "vue";
export default {
  async setup() {
    await doSomething();
    watch(foo, () => {
      /* ... */
    }); // 错误
  },
};
</script>

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

vue
<script>
import { watch } from "vue";
export default {
  async setup() {
    watch(foo, () => {
      /* ... */
    }); // 正确
    await doSomething();
  },
};
</script>

How to Use

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

json
{
  "plugins": ["vue"],
  "rules": {
    "vue/no-watch-after-await": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

版本

此规则于 v1.67.0 添加。

参考资料