Passing props to Vue.js components instantiated by Vue-router

vue.jsVue Router

vue.js Problem Overview


Suppose I have a Vue.js component like this:

var Bar = Vue.extend({
    props: ['my-props'],
    template: '<p>This is bar!</p>'
});

And I want to use it when some route in vue-router is matched like this:

router.map({
    '/bar': {
        component: Bar
    }
});

Normally in order to pass 'myProps' to the component I would do something like this:

Vue.component('my-bar', Bar);

and in the html:

<my-bar my-props="hello!"></my-bar>

In this case, the router is drawing automatically the component in the router-view element when the route is matched.

My question is, in this case, how can I pass the the props to the component?

vue.js Solutions


Solution 1 - vue.js

<router-view :some-value-to-pass="localValue"></router-view>

and in your components just add prop:

props: {
      someValueToPass: String
    },

vue-router will match prop in component

Solution 2 - vue.js

sadly non of the prev solutions actually answers the question so here is a one from quora

basically the part that docs doesn't explain well is > When props is set to true, the route.params will be set as the component props.

so what you actually need when sending the prop through the route is to assign it to the params key ex

this.$router.push({
	name: 'Home',
	params: {
		theme: 'dark'
	}
})

so the full example would be

// component
const User = {
  props: ['test'],
  template: '<div>User {{ test }}</div>'
}

// router
new VueRouter({
  routes: [
    { 
      path: '/user',
      component: User,
      name: 'user',
      props: true 
    }
  ]
})

// usage
this.$router.push({
	name: 'user',
	params: {
		test: 'hello there' // or anything you want
	}
}) 

Solution 3 - vue.js

In the router,

const router = new VueRouter({
  routes: [
    { path: 'YOUR__PATH', component: Bar, props: { authorName: 'Robert' } }
  ]
})

And inside the <Bar /> component,

var Bar = Vue.extend({
    props: ['authorName'],
    template: '<p>Hey, {{ authorName }}</p>'
});

Solution 4 - vue.js

This question is old, so I'm not sure if Function mode existed at the time the question was asked, but it can be used to pass only the correct props. It is only called on route changes, but all the Vue reactivity rules apply with whatever you pass if it is reactive data already.

// Router config:
components: {
  default: Component0,
  named1: Component1
},
props: {
  default: (route) => {
    // <router-view :prop1="$store.importantCollection"/>
    return {
      prop1: store.importantCollection
    }
  },
  named1: function(route) {
    // <router-view :anotherProp="$store.otherData"/>
    return {
      anotherProp: store.otherData
    }
  },
}

Note that this only works if your prop function is scoped so it can see the data you want to pass. The route argument provides no references to the Vue instance, Vuex, or VueRouter. Also, the named1 example demonstrates that this is not bound to any instance either. This appears to be by design, so the state is only defined by the URL. Because of these issues, it could be better to use named views that receive the correct props in the markup and let the router toggle them.

// Router config:
components:
  {
    default: Component0,
    named1: Component1
  }
<!-- Markup -->
<router-view name="default" :prop1="$store.importantCollection"/>
<router-view name="named1" :anotherProp="$store.otherData"/>

With this approach, your markup declares the intent of which views are possible and sets them up, but the router decides which ones to activate.

Solution 5 - vue.js

 const User = {
      props: ['id'],
      template: '<div>User {{ id }}</div>'
    }
    const router = new VueRouter({
      routes: [
        { path: '/user/:id', component: User, props: true }
    
        // for routes with named views, you have to define the props option for each named view:
        {
          path: '/user/:id', 
          components: { default: User, sidebar: Sidebar },
          props: { default: true, sidebar: false }
        }
      ]
    })

Object mode

const router = new VueRouter({
  routes: [
    { path: '/promotion/from-newsletter', component: Promotion, props: { newsletterPopup: false } }
  ]
})

That is the official answer. link

Solution 6 - vue.js

Use:

this.$route.MY_PROP

to get a route prop

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobertView Question on Stackoverflow
Solution 1 - vue.jslukpepView Answer on Stackoverflow
Solution 2 - vue.jsctf0View Answer on Stackoverflow
Solution 3 - vue.jsJegadesh B SView Answer on Stackoverflow
Solution 4 - vue.jsjimpView Answer on Stackoverflow
Solution 5 - vue.jsaboutqxView Answer on Stackoverflow
Solution 6 - vue.jsuser742030View Answer on Stackoverflow