You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue传递Boolean类型prop报错:预期布尔值却收到true/false字符串如何解决

报错原因

组件InternalTable的selectable属性定义为Boolean类型,你调用组件时写的selectable="true"没有加v-bind绑定符号,Vue默认会把引号内的内容解析为字符串,导致实际传入的是字符串"true",和预期的布尔类型不匹配,触发类型校验失败报错。

修复方案

方案1:绑定固定布尔值

给selectable属性前添加v-bind缩写符号:,引号内的内容会被当作JS表达式解析,得到真正的布尔值:

<template>
  <div>
    <InternalTable :participants="participants" :selectable="true"></InternalTable>
  </div>
</template>

如果需要传入false,修改为:selectable="false"即可。

方案2:简写传true

如果只需要传入固定值true,可以直接省略属性值,Vue默认会给这类空属性传布尔值true:

<template>
  <div>
    <InternalTable :participants="participants" selectable></InternalTable>
  </div>
</template>

方案3:动态绑定变量

如果需要动态控制selectable的状态,绑定一个布尔类型的响应式变量即可:

<template>
  <div>
    <InternalTable :participants="participants" :selectable="isSelectable"></InternalTable>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 可根据业务逻辑动态修改该变量的值
      isSelectable: true
    }
  }
}
</script>

内容的提问来源于stack exchange,提问作者cbln1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 13:36:03