I would like to have a dynamic breadcrumbs based on where I clicked on a category but I get an error that says my variable is undefined: TypeError: Cannot read properties of undefined (reading 'homeMenu')
. Yet in my getHomeCategory
function, the console.log of homeCategory
displays Perma'Thèque
. I don’t understand how to do it, thanks
Here is the code :
JavaScript
x
45
45
1
<script>
2
export default {
3
props: {
4
},
5
data: () => ({
6
homeMenu: "",
7
breadcrumbs: [
8
{
9
text: 'Accueil',
10
disabled: false,
11
href: '/',
12
},
13
{
14
text: this.homeMenu,
15
disabled: false,
16
href: "/" + this.homeMenu,
17
},
18
],
19
20
}),
21
computed: {
22
mapGetters({
23
console: () => console,
24
homeCategory: 'home/getCategory',
25
})
26
},
27
methods: {
28
getHomeCategory () {
29
if (this.homeCategory === "Perma'Thèque") {
30
console.log(this.homeCategory)
31
return this.homeMenu = "permatheque"
32
} else {
33
return this.homeMenu = "null"
34
}
35
},
36
37
38
},
39
mounted() {
40
if (this.plantActive) this.loading = false;
41
this.getHomeCategory()
42
}
43
}
44
</script>
45
Advertisement
Answer
data()
is declared here as an arrow function, so this
refers to the outer scope, not the Vue component instance, but even as a regular function here, this.homeMenu
won’t yet exist.
It seems that you actually want breadcrumbs
to be reactive to homeMenu
, so you should move breadcrumbs
to a computed prop:
JavaScript
1
22
22
1
export default {
2
data: () => ({
3
homeMenu: '',
4
}),
5
computed: {
6
breadcrumbs() {
7
return [
8
{
9
text: 'Accueil',
10
disabled: false,
11
href: '/',
12
},
13
{
14
text: this.homeMenu,
15
disabled: false,
16
href: '/' + this.homeMenu,
17
},
18
]
19
}
20
}
21
}
22