javascript - Does a reusable component in vuejs require an id compulsorily?
问题描述
I have a toggler component in vuejs.(CheckBox.vue) The tempate is as follows and the toggler works as long as I provide the input field an id. But I cant use id because there are multiple togglers in another component. When i remove id of checkbox, it doesnot work. What is the reason this is happening and how to fix this?
<template>
<div class="checkbox">
<label for="checkbox" class="switch">
<input type="checkbox" id="checkbox" v-model="checked" @click="onChange">
<span class="slider round"></span>
</label>
</div>
</template>
<script>
export default {
name: 'CheckBox',
props:{
defaultChecked:{
type:Boolean,default:true
}
},
data:function(){
return {
checked: this.defaultChecked
}
},
methods:{
onChange:function(){
this.checked=!this.checked;
this.$emit('status',this.checked)
}
}
}
</script>
This code works fine if I give id to checkbox and include it in another component. But when I remove id, the toggler stops to work.The component is:
//component
<div>
<CheckBox v-model="this.checkbox" @status="changeStatus" style="float:right"></CheckBox>
//Script
import CheckBox from './CheckBox.vue'
export default {
name: 'Brands',
components:{
CheckBox
},
data:function(){
return {
checkbox:true
}
},
methods:{
changeStatus:function(){
this.checkbox = !this.checkbox
}
}
}
解决方案
推荐阅读
- asp.net-core - 如何为 ResponseCacheAttribute 动态设置 Duration
- laravel-blade - 将表单选择的数据传递给 laravel7 中的控制器,返回 null
- codeigniter-4 - Codeigniter 4 检测并区分主页与其他页面
- python - Pandas 中的 Str.contain 用于所有值 NaN 的列
- c++ - 在 C++ 标准模板库 (STL) 中使用 set
- javascript - 元素出元素在JS中
- android - PackageInstaller 完成(自我)更新后启动应用程序
- javascript - 微服务、异步调用、在兄弟组件之间传输数据:将它们放在一起
- google-calendar-api - 如何通过 API 从主日历(在 GSuit 中)获取已删除的事件?
- api - 错误:[mapper_parsing_exception] 根映射定义具有不受支持的参数:“Elasticserach”