javascript - Vue js v-if 在共享切换按钮上进行条件渲染

标签 javascript json vuejs2 vue-component vuex

        //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>

可以切换"is"或“否”选项,如果选择"is",则子组件将被渲染,否则将保持隐藏。 我正在尝试使用条件,以便使用指令 v-ifv-show 将子组件显示到父组件中,但我找不到方法将 bool 从子组件发送到父组件。

最佳答案

希望这有帮助!!

// 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>

关于javascript - Vue js v-if 在共享切换按钮上进行条件渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52138927/

相关文章:

javascript - can-model 无法从 .json 文件获取数据

Vue.js:根据 'data' 变化重新计算并重新渲染 'prop'。这是更好的方法吗?

vue.js - Vuejs - - 在父级请求 ajax 后在子级中分配 prop 值。

javascript - Vue 2.X - 在组件中等待对比异步数据源的好方法

Javascript:处理常量和跨浏览器兼容性

javascript - 以 chrome 扩展形式转换时,事件函数不会在控制台中打印

javascript - 渐进式WA : Getting Web Share API to work in development

arrays - 用shell读取json数组变量

jquery - Marklogic 中是否有简单的 XQuery 可以从简单的 XML 序列中生成 JSON 输出,以便与 JQuery 自动竞争一起使用

c# - 使用 C# 的数据库结果的 JSON 数组格式