简体   繁体   English

如何在Vue js的父组件和3个子组件中使用全局变量

[英]How to use global variable in parent component and 3 child component in Vue js

I have created 3 steps from using 3 child components. 我通过使用3个子组件创建了3个步骤。 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? 有人可以帮忙吗?

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 您的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 步骤1组件

  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 步骤2组件

 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 步骤3组件

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. 每个组件都可以通过将其名称传递给:step =“ step”并在每个app-step组件中注册一个props:['step']来从父级接收step属性,然后在step组件内部使用此属性属性并知道当前值(在当前示例中,我不使用它,但是我展示了如何接收它)计算后的每个组件或您的业务逻辑是什么,都可以向父级发出想要的更改应用于该步骤。 The component should notify the parent by running this command this.$emit('stepChanged','<value of the step>') . 组件应通过运行此命令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 为了使父组件侦听更改,它应向每个组件注册一个名为“ stepChanged”的处理程序以及将被调用的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM