在使用组件绑定属性的时候,如果直接是属性名称等于数字,在组件props获取这个属性时,是字符串类型,而且vue也不会检查出来提示错误。看以下实例代码:
使用组件
<a-component data="123"></a-component>
a-component组件里的js
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
props: {
data: {
type: Number,
default: 0,
},
},
setup(props) {
console.log(typeof props.data) // 会发现是返回:string
// 在封装vant的NavBar时,父组件传的active,组件里的菜单就是没选中,原来获取的不是数字类型
// 需要:active = ref(Number(props.selected))
// 或者 :data="123" 才能是数字类型,跟:status="true",,传boolean类型同一个道理
// <a-component :data="123"></a-component>
},
})
</script>