0

I am new to VueJs and I have a problem with two components. I am trying to display a component with vuejs router and I must pass properties to that component and since it's not a child of that component (it's in different directory), how can I pass data to that component? For example:

This is a Parent component:

 <template>
    <div class="container">
       <router-link to="/Form"></router-link>
    </div>
</template> 
<script>
  export default{
  data(){
    return{
      values: {val1: 123, val2: 321}
    }
  }
}
</script>

This is a components that needs properties, Form component:

 <template>
    <div class="container">
       <form>
         <input type="text" v-model="values.val1"/>
         <input type="number" v-model="values.val2"/>
       </form>
    </div>
</template> 
<script>
  export default{
  props: {
    values: {
      type: Object
    }
  }
}
</script>
Bernard Doci
  • 739
  • 3
  • 14
  • 23

1 Answers1

1

You could pass the values as the query object:

<router-link :to="{ path: '/Form', query: values }"></router-link>

And have the Form component check for the $route.query values instead of using props:

<template>
  <div class="container">
    <form>
      <input type="text" v-model="values.val1"/>
      <input type="number" v-model="values.val2"/>
    </form>
  </div>
</template> 
<script>
export default {
  data() {
    return {
      values: this.$route.query
    }
  }        
}
</script>

Note that these values will be added to the URL as a query string.


Otherwise, I'd take a look at this post. Or maybe look into Vuex.

thanksd
  • 54,176
  • 22
  • 157
  • 150