Vue js v-如果在共享切换按钮上进行同步渲染

问题描述 投票:0回答:1
        //PARENT COMPONENT

     <template>
          ....
        <div class="input-wrapper">//Toggle button container
            <label class="input-label">
              SELECT YES OR NOT
            </label>
            <toggle //child component, toggle button
            :options="shipping"
          />
        </div>
        <div
           v-if="destiny[0].value"
           class="input-wrapper">
            <label class="input-label">
              IF YES THIS CONTAINER WILL BE DISPLAYED
            </label>
            <toggle
              :options="Options"
              />
         </div>

                .....
        </template>
        <script>
        import Toggle from "....";
                export default {
                  components: {
                    Toggle,
                  },
                  data: function () {
                    return {
                      destiny: [{
                        label: 'Yes',
                        value: true
                      },
                      {
                        label: 'No',
                        value: false
                      }
                      ],
                      Options: [{
                        label: 'A',
                        value: 'a'
                      },
                      {
                        label: 'B',
                        value: 'b'
                      },
                      {
                        label: 'C',
                        value: 'c'
                      }]
                    }
                  }
                }
               </script>


        ///CHILD COMPONENT


        <template>
          <div class="toggle">
             <button
               v-for="option in options"
               :key="option.value"
               :class="{
                 active: option.value === value
               }"
               class="btn"
               @click="() => toggleHandler(option.value)">{{ option.label }} . 
             </button>
          </div>
        </template>

            <script>

            export default {
              props: {
                options: {
                  type: Array,
                  required: true
                }
              },
              data: function () {
                return {
                  value: this.options[0].value
                }
              },
              methods: {
                toggleHandler (value) {
                  this.$emit('input', value)
                  this.value = value
                }
              }
            }
            </script>

切换到选项YES或NOT,如果选择yes,子组件将被渲染,否则将保持隐藏。我正在尝试使用条件来使用指令v-if或v-show将子组件显示到父组件中,但是我找不到将子组件的布尔值发送到父组件的方法。

javascript json vuejs2 vue-component vuex
1个回答
1
投票

希望这可以帮助!!

// CHILD
Vue.component('child', {
	template: '<div>TOGGLE:- <input type="checkbox" @click="emit"/></div>',
	data() {
		return {
			checked: false
		};
	},
	methods: {
		emit: function() {
			this.checked = !this.checked;
			this.$emit('event_child', this.checked);
		}
	}
});

// PARENT
var vm = new Vue({
	el: '#app',
	data: function() {
		return {
			toggleStatus: false
		}
	},
	methods: {
		eventChild: function(checked) {
			this.toggleStatus = checked;
		},
	}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.js"></script>

<div id="app">	
	<child v-on:event_child="eventChild"></child>
  <div id="toggle">TOGGLE STATUS => {{toggleStatus}}</div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.