问题描述
在 Vue3 + ElementUI + TypeScript 开发中,给标签设置样式属性 style 时,控制台提示类型检查失败:
[Vue warn]: Invalid prop: type check failed for prop "style". Expected Object, got String with value "height:calc(100vh - 252px) !important;".
该警告表明赋值和接收参数值的类型不匹配。
原因分析
Vue 3 期望 :style 绑定的是一个对象(Object),但你传递的是一个字符串(String)。你需要将样式绑定改为对象形式。
当前写法 :style="newTableHeight()" 返回的是一个字符串,而 Vue 3 要求样式绑定应该是一个对象。
解决方案
1. 修改方法返回值
修改你的 newTableHeight 方法,让它返回一个样式对象而不是字符串:
methods: {
newTableHeight() {
return { height: 'calc(100vh - 252px)' }
}
}
或者直接在值上添加 !important(虽然这不是官方推荐的做法):
methods: {
newTableHeight() {
return { height: 'calc(100vh - 252px) !important' }
}
}
2. 模板中使用对象语法
在模板中直接使用对象语法:
<div :style="{ height: 'calc(100vh - 252px)' }"></div>
3. 使用计算属性
如果必须使用动态计算,建议使用计算属性:
computed: {
tableStyle() {
{ : }
}
}


