Dynamic breadcrumbs in Vue JS

Dynamic breadcrumbs in Vue JS

Today, We want to share with you Dynamic breadcrumbs in Vue JS.In this post we will show you Displaying application Breadcrumbs in Vue.js, hear for Creating a breadcrumb from the current path – Vue.js 2.x by Example we will give you demo and example for implement.In this post, we will learn about Handling breadcrumbs with VueX in a VueJS Single Page Application with an example.

Dynamic breadcrumbs in Vue JS

There are the Following The simple About Dynamic breadcrumbs in Vue JS Full Information With Example and source code.

  how to create custom WordPress Plugin from scratch

As I will cover this Post with live Working example to develop Dynamic Breadcrumbs with vuex2 & vue-router 2, so vuetify breadcrumbs vue router for this example is following below.

Automated Breadcrumbs with VueJS

Step 1: Vuejs Part

const state = {
  products: [{id: 12, name: "Iphone"},{id: 13, name: "Mobile"}],
  activeProductId: null,
}
const mutations = {
  setActiveProduct: (state, payload) => {
    state.activeProductId = payload.id
  }
}
const getters = {
  activeProduct: ({products, activeProductId}) => {
    return products.find((u) => u.id === activeProductId) || null
  }
}
const store = new Vuex.Store({
	state,
  getters,
  mutations
})

const Shop =  {
	created() { this.loadData() },
  watch: {
  	$route() { this.loadData() }
  },
	computed: {
  	product() { return this.$store.getters.activeProduct }
  },
  methods: {
  	loadData() {
    	this.$store.commit('setActiveProduct', {id: null})
    	setTimeout(() => { // simulate async data fetching...
     this.$store.commit('setActiveProduct', {id: this.$route.params.id})
    }, 2000)
    }
  },
	template: '<div v-if="product">Shop for: {{product.name}}</div>'
}

const routes = [
	{
  	path: '/',
    name: 'root',
    component: {template: '<div>Root Path</div>'},
    meta: { bcLinkText: 'Default Page'},
  },
	{
  	path: '/products',
    name: 'products',
    component: { template: '#products'},
    meta: { bcLinkText: "Products"},
    children: [
      {
      	path: ':id',
        name: 'shop',
        component: Shop,
        meta: {
        	breadcrumbs: true,
          bcGetter: 'activeProduct', 
          bcLinkText: product =>  product.name, 
          bcLoadingText: 'Loading Productname...'
        },
      }
    ]
  }
]

const router = new VueRouter({
	mode: 'history',
  base: '/_display/',
  routes
})


Vue.component('breadcrumb', {
	props: ['route'],
  template: '#breadcrumb',
  beforeCreate() {
    this.$options.computed.value = function() { 
      if (this.route.meta.bcGetter) {
        return this.$store.getters[this.route.meta.bcGetter] 
      } else {
        return null
      }
    }
  },
  computed: {
    formattedValue() { return this.route.meta.bcLinkText(this.value) },
    loadingText() {
    	const loadingText = this.route.meta.bcLoadingText
      return loadingText ? loadingText : 'Loading...'
    }
  }
})

new Vue({
  el: '#app',
  router,
  store
})

Step 2: HTML Part

<div id="app">
  <h2>Dynamic Breadcrumbs</h2>
  
  <div>
    <h5>Breadcrumbs:</h5>
    <ul class="menu">
      <li v-for="(route, index) in $route.matched">
        <breadcrumb :route="route"></breadcrumb>
      </li>
    </ul>
  </div>
  
  <hr>
  <router-link to="/products">Open Products Route</router-link>
  <router-view></router-view>
  
  <hr>
  <h5>$data:</h5>
  <pre>
    {{$store.state}}
  </pre>
</div>

<template id="products">
  <div>
    <h5>Products</h5>
    <ul>
      <li v-for="product in $store.state.products">
        <router-link :to="{name: 'shop', params: {id: product.id}}">
         Show Product {{product.name}}
        </router-link>
      </li>
    </ul>
    <router-view></router-view>
  </div>
</template>

<template id="breadcrumb">
  <span>
    <router-link v-if="!route.meta.breadcrumbs" :to="{name: route.name}">
      {{route.meta.bcLinkText}}
    </router-link>
    <router-link v-if="route.meta.breadcrumbs" :to= "{name: route.name, params: {id: $route.params.id}}">
      <template v-if="value">
        {{formattedValue}}  
      </template>
      <template v-if="!value">
        {{loadingText}}
      </template>
    </router-link>
  </span>
</template>

bootstrap vue breadcrumb example
Web Programming Tutorials Example with Demo

Read :

  C#.Net Enum(Enumeration) Tutorial with Examples

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Dynamic breadcrumbs in Vue JS.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Reply

avatar
  Subscribe  
Notify of