最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How make dynamic breadcrumbs in vue.js? - Stack Overflow

programmeradmin2浏览0评论

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 :

<script>
    export default {
        props: {
        },
        data: () => ({
            homeMenu: "",
            breadcrumbs: [
                {
                    text: 'Accueil',
                    disabled: false,
                    href: '/',
                },
                {
                    text: this.homeMenu,
                    disabled: false,
                    href: "/" + this.homeMenu,
                },
            ],
            
        }),
        puted: {
            ...mapGetters({
                console: () => console,
                homeCategory: 'home/getCategory',    
            })
        },
        methods: {
            getHomeCategory ()  {
                if (this.homeCategory === "Perma'Thèque") {
                    console.log(this.homeCategory)
                    return this.homeMenu = "permatheque"
                } else {
                    return this.homeMenu = "null"
                }
            },

            
        },
        mounted() {
            if (this.plantActive) this.loading = false;
            this.getHomeCategory()
        }
    }
</script>

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 :

<script>
    export default {
        props: {
        },
        data: () => ({
            homeMenu: "",
            breadcrumbs: [
                {
                    text: 'Accueil',
                    disabled: false,
                    href: '/',
                },
                {
                    text: this.homeMenu,
                    disabled: false,
                    href: "/" + this.homeMenu,
                },
            ],
            
        }),
        puted: {
            ...mapGetters({
                console: () => console,
                homeCategory: 'home/getCategory',    
            })
        },
        methods: {
            getHomeCategory ()  {
                if (this.homeCategory === "Perma'Thèque") {
                    console.log(this.homeCategory)
                    return this.homeMenu = "permatheque"
                } else {
                    return this.homeMenu = "null"
                }
            },

            
        },
        mounted() {
            if (this.plantActive) this.loading = false;
            this.getHomeCategory()
        }
    }
</script>
Share Improve this question edited Feb 3, 2022 at 20:41 tony19 139k23 gold badges277 silver badges347 bronze badges asked Feb 3, 2022 at 17:30 RitalCharmant RitalCharmant 1393 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

data() is declared here as an arrow function, so this refers to the outer scope, not the Vue ponent 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 puted prop:

export default {
  data: () => ({
    homeMenu: '',
  }),
  puted: {
    breadcrumbs() {
      return [
        {
          text: 'Accueil',
          disabled: false,
          href: '/',
        },
        {
          text: this.homeMenu,
          disabled: false,
          href: '/' + this.homeMenu,
        },
      ]
    }
  }
}

demo

I used Element Plus UI and my mind was blown. Check out my blog and the codes. Element Plus is a free library that is great for Vue JS. They have very nice UI for breadcrumb and can be implemented with v-for loop. https://medium./@samchowdhury/create-a-breadcrumb-with-vue-js-and-element-plus-ui-f3e2fde50a3e

发布评论

评论列表(0)

  1. 暂无评论