vue/no-lifecycle-after-await Correctness
作用
禁止在 await 之后注册生命周期钩子。
为什么这不好?
生命周期钩子必须在 setup() 执行期间同步注册。 如果在 await 语句之后调用生命周期钩子,可能注册得太晚, 并且可能无法按预期工作。
示例
以下是此规则的错误代码示例:
vue
<script>
import { onMounted } from "vue";
export default {
async setup() {
await doSomething();
onMounted(() => {
/* ... */
}); // 错误
},
};
</script>以下是此规则的正确代码示例:
vue
<script>
import { onMounted } from "vue";
export default {
async setup() {
onMounted(() => {
/* ... */
}); // 正确
await doSomething();
},
};
</script>如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["vue"],
"rules": {
"vue/no-lifecycle-after-await": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["vue"],
rules: {
"vue/no-lifecycle-after-await": "error",
},
});bash
oxlint --deny vue/no-lifecycle-after-await --vue-plugin版本
此规则于 v1.39.0 中添加。
