首页 > 解决方案 > 如果没有传递任何内容,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 端之前将这些值定义为空?

标签: javascripttypescriptvue.js

解决方案


您的对象道具的默认值可能是:

 @Prop({ default: () => ({id:0,name:'',type:''}) }) discount!: Discount;

推荐阅读