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

前端 未结 6 1085
日久生厌
日久生厌 2020-11-29 04:15

Suppose I have a Vue.js component like this:

var Bar = Vue.extend({
    props: [\'my-props\'],
    template: \'

This is bar!

\' });
<
6条回答
  •  佛祖请我去吃肉
    2020-11-29 04:46

    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: '
    User {{ test }}
    ' } // 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 } })

提交回复
热议问题