javascript - 如果没有传递任何内容,Vue.js 将 prop 值设置为空
问题描述
所以我有这个折扣界面:
export interface Discount {
id: number
name: string
type: string
}
我在 Vue.js 应用程序中的道具上使用它,如下所示:
export default class DiscountsEdit extends Vue {
@Prop({ default: () => {} }) discount!: Discount;
}
我使用 twig 模板引擎并将空折扣数组传递给 Vue 组件。我希望整个折扣对象具有空变量值,例如:
id: 0
name: ''
type: ''
是否可以在 Vue.js 端自动设置,或者我必须在将其发送到 Vue 端之前将这些值定义为空?
解决方案
您的对象道具的默认值可能是:
@Prop({ default: () => ({id:0,name:'',type:''}) }) discount!: Discount;
推荐阅读
- python - 如何检查一个项目是否在 PHP 的 list.txt 中?
- amazon-s3 - AWS S3 将参数添加到签名的 url
- java - 2个值之间的硒比较失败
- css - React&TypeScript:样式表对象的类型是什么?
- angular - Angular Material Stepper,每个步骤都有单独的组件 - ExpressionChangedAfterItHasBeenCheckedError
- java - Spring MethodArgumentNotValidException 字段始终为空
- python - Impyla 连接。无法启动 SASL。没有可用的机制
- python - 使用绝对路径配置 pytest.ini
- php - 如何从 RabbitMQ que 获取消息并使用 twillio API 将其发送到用户手机
- laravel - 数组项的 Laravel 条件验证规则