Skip to content

noVueThisInBeforeRouteEnter (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noVueThisInBeforeRouteEnter": "error"
}
}
}
}

Disallow this in the beforeRouteEnter navigation guard of a Vue component.

Vue Router calls beforeRouteEnter before it creates the component, so the component doesn’t exist yet and this is undefined. Reading a property through this, such as this.loadUser(), throws a TypeError when the user navigates to the page.

To use the component, pass a callback to next. Vue Router calls it with the component once the navigation is confirmed.

The rule checks beforeRouteEnter when it is written as a method or a function expression, in export default {}, defineComponent(), createApp(), and defineOptions() in <script setup>.

<script>
export default {
beforeRouteEnter(to, from, next) {
this.loadUser(to.params.id);
next();
},
};
</script>
code-block.vue:4:5 lint/nursery/noVueThisInBeforeRouteEnter ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ this is used in beforeRouteEnter, where it is undefined.

2 │ export default {
3 │ beforeRouteEnter(to, from, next) {
> 4 │ this.loadUser(to.params.id);
│ ^^^^
5 │ next();
6 │ },

ℹ Vue Router calls beforeRouteEnter before it creates the component, so the component can't be reached through this.

ℹ Pass a callback to next to use the component once it is created, for example next((vm) => vm.loadData()).

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

<script>
export default {
beforeRouteEnter(to, from, next) {
next((vm) => {
vm.loadUser(to.params.id);
});
},
beforeRouteUpdate(to) {
this.loadUser(to.params.id);
},
};
</script>