Vue.js - 将二进制字符串绑定到复选框输入的组件

时间:2017-01-09 07:20:15

标签: vue.js vuejs2

在Vue.js中,我如何编写一个绑定到1和0字符串的组件,例如&#34; 111001011&#34;,并将这些数据作为一组命名复选框表示给用户?< / p>

1 个答案:

答案 0 :(得分:0)

也许这就是你要找的东西:

App.vue

<template>
    <div id="app">
        <checkboxes :binary="binary"></checkboxes>
    </div>
</template>

<script>
import Checkboxes from './components/Checkboxes'

export default {
    name: 'app',
    data(){
        return {
            binary: "11001011"
        };
    },
    components: {
        Checkboxes
    }
}
</script>

Checkboxes.vue:

<template>
    <div>
        <ul>
            <li v-for="position in binary.length">
                <label>
                    <input type="checkbox" :name="binary[position - 1]" :checked="binary[position - 1] == '1' ? true : false"> {{ binary[position - 1] }}
                </label>
            </li>
        </ul>
    </div>
</template>

<script>
export default {
    name: 'checkboxes',
    props: {
        binary: {
            required: true,
            type: String
        }
    }
}
</script>

这将通过字符串长度,并且每个字符将根据二进制值(1/0)标记为已选中/未选中

结果:

enter image description here