Vue, is there a way to pass data between routes without URL params?

前端 未结 2 381
醉酒成梦
醉酒成梦 2020-12-31 02:55

I am looking how to pass data secretly between two separate components (not parent and child) without using URL params in my Vue2 app. This doesn\'t mean I am passing secret

2条回答
  •  情歌与酒
    2020-12-31 03:28

    1. make props: true for the destination route -- in the index.js file of router

        {
          path: '/home',
          name: 'home',
          component: taskChooser,
          props: true,
          }

    1. define prop in the component e.g props: ['myprop'], - note the quotes

    2. copy the variable you want to pass from the source route into the same name as your prop - in this case myprop

    myprop = theVariableThatYouWantToPass
    
    this.$router.replace({name:'home', params:{myprop}});

    Make sure that the name of prop and variable are same - the prop is in quotes.

    It's working for me.

提交回复
热议问题