动态添加时在第一个组件上单击的组件触发事件

时间:2018-09-30 14:25:39

标签: javascript vue.js components vue-component

我有这个组件

<template>
    <div class="row py-2">
        <div class="col-sm-12">
            <v-checkbox @clicked="changeStatus($event)" :val="checkFont" :key="'checkfont'">
                <span>Activar/Desactivar</span>
            </v-checkbox>
        </div>
        <transition name="fade">
            <div class="col-sm-12" v-if="checkFont == true">
                <div v-for="(type, key) in types">
                    <div class="my-2">
                        <h5 class="text-capitalize">{{ type.label }}</h5>
                    </div>  
                    <div class="my-2">
                        <v-selectpicker :input-name="fieldName(type.label)" :properties="fonts" :has-addon="false" field="family" :notlang="true" @optionselected="getFont($event, key)"></v-selectpicker>
                    </div>
                    <div class="" v-if="type.font !== ''">
                        <div v-for="(v, index) in getVariants(key)" :key="index">
                            <v-checkbox @clicked="setVariant($event, key, v)" :val="variantText(v)" :key="index">
                                <span>{{ variantText(v) }}</span>
                            </v-checkbox>
                        </div>
                    </div>


                </div>
            </div>
        </transition>
    </div>
</template>
<script>
    const SelectPicker = () => import('../admin/selectpicker')
    const CustomCheckbox = () => import('./customCheckbox')
    export default {
        data : function() {
            return {
                fonts   : [{family : ''}],
                types   : {
                    headers : {
                        label   : 'Titulos',
                        font : '',
                        variant : ''
                    },
                    paragraphs : {
                        label   : 'parrafos',
                        font    : '',
                        variant : '',
                    },
                    links      : {
                        label   : 'Links',
                        font    : '',
                        variant : '',
                    }
                },
                checkFont : false
            }
        },
        created : function() {
            this.getFonts();
        },
        components : {
            'v-selectpicker' : SelectPicker,
            'v-checkbox' : CustomCheckbox
        },
        methods : {
            getFonts : function() {
                axios.get('https://www.googleapis.com/webfonts/v1/webfonts?sort=popularity&key=AIzaSyBUjWSsTwBeOFXwHYga8YQZIDauLiS8Gy8').then((response) => {
                    this.fonts = response.data.items;
                });
            },
            fieldName : function(field) {
                return field.toLowerCase()
            },
            getFont : function($event, key) {
                this.types[key].font = $event.option;
                this.getVariants(key);
            },
            changeStatus : function($event) {
                console.log($event)
                this.checkFont   = !this.checkFont;
            },
            getVariants : function(key) {
                return this.fonts.filter((element) => element.family == this.types[key].font)[0].variants;
            },
            setVariant : function($event, key, v) {
                console.log('variant clicked');
                console.log($event);
                this.types[key].variant = v;
            },
            variantText : function(v) {
                return v.indexOf('00') !== -1 ? v.split('00').join('00 ') : v;
            }
        }
    }
</script>

这是v-checkbox组件。

<template>
    <div class="cntr">
        <label for="cbx" class="label-cbx">
            <input id="cbx" type="checkbox" :model="val" class="invisible" @change="checked = checkCheckbox(checked)" :checked="checked">
            <div class="checkbox">
                <svg width="20px" height="20px" viewBox="0 0 20 20">
                    <path d="M3,1 L17,1 L17,1 C18.1045695,1 19,1.8954305 19,3 L19,17 L19,17 C19,18.1045695 18.1045695,19 17,19 L3,19 L3,19 C1.8954305,19 1,18.1045695 1,17 L1,3 L1,3 C1,1.8954305 1.8954305,1 3,1 Z"></path>
                    <polyline points="4 11 8 15 16 6"></polyline>
                </svg>
            </div>
            <slot></slot>
        </label>
    </div>
</template>
<script>
    export default {
        props : ['val'],
        data : function() {
            let data = {
                checked : false
            };
            return data
        },
        methods : {
            checkCheckbox : function(checkbox) {
                this.$emit('clicked', {
                    value : this.val
                });
                let status = eval(checkbox) == true ? false : true;
                return status;
            }
        }
    }
</script>

问题出在v-checkbox中,一切正常,直到我单击v-for="(v, index) in getVariants(key)" :key="index"触发的the v-checkbox :key="'checkfont'"中的动态v-checkbox之一,因此{{{ 1}}属性值更改

我正在使用vue devtools检查组件,我可以看到checkFont属性具有正确的值,但是当单击该事件时会发出一个布尔值,然后触发:val,为什么会这样发生?我不能使用这样的组件吗?我该如何解决?

1 个答案:

答案 0 :(得分:1)

我意识到问题出在id的{​​{1}}上,每个checkbox的ID为checkbox,因此它总是在点击时调用第一个元素