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>
2
  • Where is the router-view? Commented Sep 11, 2017 at 20:08
  • It's in App.vue. Commented Sep 11, 2017 at 20:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.