Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [Bug]: [Checkbox]组件的checked属性在非group中不生效 #1769

Closed
AcWrong02 opened this issue Jul 23, 2024 · 7 comments
Closed

🐛 [Bug]: [Checkbox]组件的checked属性在非group中不生效 #1769

AcWrong02 opened this issue Jul 23, 2024 · 7 comments
Assignees
Labels
bug Something isn't working done 已完成/已修复

Comments

@AcWrong02
Copy link
Collaborator

Version

latest

Vue Version

3

Link to minimal reproduction

<template>
    <tiny-checkbox label="复选框1" name="name2" :checked="true"></tiny-checkbox>
</template>

<script>
import { Checkbox, CheckboxGroup } from '@opentiny/vue'

export default {
  components: {
    TinyCheckbox: Checkbox,
    TinyCheckboxGroup: CheckboxGroup
  },
}
</script>

Step to reproduce

复制到Playground中,发现设置checked为true无效

What is expected

在某些场景下,我需要对单独的checkbox使用checked属性,期望它可以生效。

What is actually happening

单独的checkbox使用checked属性不生效

What is your project name

null

Any additional comments (optional)

No response

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


Title: 🐛 [Bug]: The checked attribute of the [Checkbox] component does not take effect in non-groups

@AcWrong02 AcWrong02 added the bug Something isn't working label Jul 23, 2024
@shenjunjian shenjunjian added the need discussion 需要讨论 label Jul 31, 2024
@shenjunjian
Copy link
Collaborator

"单独的checkbox使用checked属性不生效"

单独使用的话, 建议用 v-model 绑定即可。 没必要使用checked属性。 需要讨论

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


"Using the checked attribute for a separate checkbox does not take effect"

If used alone, it is recommended to use v-model binding. There is no need to use the checked attribute. Need to discuss

@AcWrong02
Copy link
Collaborator Author

AcWrong02 commented Jul 31, 2024

@shenjunjian 场景是这样子的:我的子组件中用到了父组件传递过来的属性isChecked(在某个对象上),我需要在它改变的时候给父组件发生某个事件,直接用v-model将该属性绑定到checkbox上,就违反了Vue的单向数据流(https://cn.vuejs.org/guide/components/props.html#mutating-object-array-props)。

//parent.vue
<template>
  <Children :oneObj="obj"/>
</template>
<script setup>
import {ref} from 'vue'
const objArray = [{
   ....
 isChecked: false,
}]
</script>
//children.vue
<template>
   <div v-for="item in oneObj">
       <tinyCheckbox v-model="item.isChecked" @change="handleChange"/>
   </div>
</template>
<script setup>
const props = defineProps({
    oneObj: Array
})
const handleChange = ()=>{
   // some code
}
</script>

在使用其他组件库的时候,我是用check属性来规避这个问题的

@vaebe
Copy link
Contributor

vaebe commented Aug 12, 2024

image

也许可以这样, 把 v-model 展开

@shenjunjian
Copy link
Collaborator

shenjunjian commented Jan 6, 2025

我试了最新的代码, checked属性是可以正常使用了
“直接用v-model将该属性绑定到checkbox上,就违反了Vue的单向数据流”,这个场景报警告很常见,
v-model建议只绑定上下文的状态值,不建议绑定props值。

  • 可以在组件内克隆一个变量用于绑定,
  • 也可以将v-model拆开用, 通知父组件更新某个值,以符合vue的规范建议。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


I tried the latest code and the checked attribute can be used normally. I don't know what was changed.

"Directly using v-model to bind this property to the checkbox violates Vue's one-way data flow." Warnings are very common in this scenario.
v-model recommends only binding the state value of the context, and does not recommend binding props values.

  • You can clone a variable within the component for binding,
  • You can also disassemble v-model and notify the parent component to update a certain value to comply with Vue's specification recommendations.

@shenjunjian shenjunjian added done 已完成/已修复 and removed need discussion 需要讨论 labels Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working done 已完成/已修复
Projects
None yet
Development

No branches or pull requests

4 participants