0

I have created 3 steps from using 3 child components. All these three components are linked in one parent component.

What I need to do is:

  • Show these three forms as steps depending on step number which I need to be a global variable.
  • Update depends on each form call to action button.

Can anyone help on this?

2
  • The answer is on your question hehehe check this example vuejs.org/v2/guide/… ... you do that for each component, add a prop stepCount, then just pass the stepCount global data... also, your code seems to already be doing what you want, maybe try with v-show instead of v-if, in case you are having rendering issues. Commented Sep 12, 2018 at 16:54
  • I don't understand your question. Each component would be shown if the stepCount equals the associated int value. What isn't working as you expect it to? Commented Sep 12, 2018 at 17:43

1 Answer 1

2

You will need 2 things

  1. To pass a property to the components in order to know the current step value

  2. To emit an event from each component so the parent component will get notified about the new value that the step should be updated

I write the code below and give the explanation later

Your html

 <div id="app">
 </div>

Parent Component

 var instance = new Vue({
   el:'#app',
   data:{
     step:1
   },
   methods:{
     stepChanged:function(step){
       alert('Moving to step:'+step);
      this.step = step;
    }
  },
  template:`
  <div>
 <app-stepone v-if="step==1" v-on:stepChanged="stepChanged" :step="step" > 
  </app-stepone>

 <app-steptwo v-if="step==2" v-on:stepChanged="stepChanged" :step="step" > 
 </app-steptwo>

 <app-stepthree v-if="step==3" v-on:stepChanged="stepChanged" :step="step"> 
 </app-stepthree>
 </div>
 `
  });

Step 1 component

  Vue.component('app-stepone',{
   props:['step'],
   data:function(){
   return {};
  },
   methods:{
    moveStep(){
     this.$emit('stepChanged',2)
   }
  },
   template:`<div>We are in Step 1 - {{step}}<br /><button v- 
    on:click="moveStep()">Move to Step 2</button></div>`
 });

Step 2 Component

 Vue.component('app-steptwo',{
   props:['step'],
   data:function(){
    return {};
  },
  methods:{
    moveStep(){
      this.$emit('stepChanged',3)
   }
  },
  template:`<div>We are in Step 2 - {{step}}<br /><button v- 
 on:click="moveStep">Move to Step 3</button></div>`
 });

Step 3 Component

Vue.component('app-stepthree',{
  props:['step'],
  data:function(){
   return {};
 },
  methods:{
   moveStep(){
     this.$emit('stepChanged',1)
  }
 },
 template:`<div>We are in Step 3 - {{step}}
   <br />
    <button v-on:click="moveStep">Move to first step</button>
    </div>`
});

Each component can receive the step property from the parent by just passing to it with the name :step="step" and registering a props:['step'] in every app-step component and then inside the step component you can use this property and know the current value (in the current example i do not use it, but i show how you can receive it ) Each component after it does its calculation or whatever your business logic is, can emit to the parent the change that would like to be applied for the step. The component should notify the parent by running this command this.$emit('stepChanged','<value of the step>') . In order for the parent component to listen for the change, it should register to each component a handler that will have the name 'stepChanged' and the method that will be called

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

2 Comments

You can check the working example at codepen codepen.io/anon/pen/xajYLz?editors=1111
Thank you gijoe! You save my day. Explained perfectly :)

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.