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

前端 未结 2 380
醉酒成梦
醉酒成梦 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:07

    Thanks @Mayank for pointing me in the correct direction.

    Here is the correct syntax that worked for me.

    1. Notice the props in In router index

      {
        path: '/componentPath',
        name: 'componentName',
        props: {
           header: true,
           content: true
        },
      }
      
    2. In the component you are redirecting to, define the props as following:

      props: {
        myProperty: {
          type: 
        },
      }
      
    3. Perform redirect as following:

      this.$router.push({
        name: 'componentName',
        params: {
          myProperty: 
        }
      })
      
    4. Access props with the this. convention from created or in later lifecycle event.

    In this case, the variable name and property name do not have to be the same as it is a simple map. That naming convention would be a curious design choice anyway.

提交回复
热议问题