noVueThisInBeforeRouteEnter (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/noVueThisInBeforeRouteEnter - This rule doesn’t have a fix.
- The default severity of this rule is error.
- This rule belongs to the following domains:
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noVueThisInBeforeRouteEnter": "error" } } }}Description
Section titled “Description”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>.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”<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>Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.