我有一个将对象传递给子组件的vue实例。子组件有一个复选框,单击该复选框时,调用vue实例处理的事件以更新传递给子组件的父对象。基于vue documentation我认为这会导致子组件更新相关字段。但是,当我单击复选框时,日期字段不会像我期望的那样更新。在下图中,当我选中管理名称复选框时,我希望当天出现,但我没有看到任何日期。我在这里缺少什么?
new Vue({
el: "#evaluations-app",
data: {
evaluation: new Evaluation()
},
methods: {
updateEmployeeSO: function (newSO, newSODate) {
this.evaluation.EmployeeSO = newSO;
this.evaluation.EmployeeSODate = newSODate;
},
updateReviewerSO: function (newSO, newSODate) {
this.evaluation.ReviewerSO = newSO;
this.evaluation.ReviewerSODate = newSODate;
},
updateManagementSO: function (newSO, newSODate) {
this.evaluation.ManagementSO = newSO;
this.evaluation.ManagementSODate = newSODate;
}
});
Vue.component('sign-off', {
props: ['initEvaluation', 'perspective'],
template: `
<div class="sign-off-comp">
<div class="sign-off-item">
<div class="sign-off-field-1 col-1">{{evaluation.EmployeeName}}</div>
<input :disabled="!enableEmployeeSO" v-model="evaluation.EmployeeSO" class="sign-off-field-2 col-2" type="checkbox" @click="EmployeeSOChanged"/>
<div class="sign-off-field-3 col-3">{{employeeSODate}}</div>
</div>
<div class="sign-off-item">
<div class="sign-off-field-1 col-1">{{evaluation.ReviewerName}}</div>
<input :disabled="!enableReviewerSO" v-model="evaluation.ReviewerSO" class="sign-off-field-2 col-2" type="checkbox" @click="ReviewerSOChanged"/>
<div class="sign-off-field-3 col-3">{{reviewerSODate}}</div>
</div>
<div class="sign-off-item">
<div class="sign-off-field-1 col-1">{{evaluation.ManagementName}}</div>
<input :disabled="!enableManagementSO" v-model="evaluation.ManagementSO" class="sign-off-field-2 col-2" type="checkbox" @click="ManagementSOChanged"/>
<div class="sign-off-field-3 col-3">{{managementSODate}}</div>
</div>
</div>
`,
data: function () {
return {
evaluation: this.initEvaluation,
employeeClicked: false,
reviewerClicked: false,
managementClicked: false,
currentCommentSource: this.perspective
}
},
methods: {
EmployeeSOChanged: function () {
this.employeeClicked = true;
//this.evaluation.EmployeeSODate == null || this.evaluation.EmployeeSODate == "" ? this.evaluation.EmployeeSODate = Helpers.getCurrentDate() : this.evaluation.EmployeeSODate = "";
this.$emit('employee-so-changed', this.evaluation.EmployeeSO, this.evaluation.EmployeeSODate);
},
ReviewerSOChanged: function () {
this.reviewerClicked = true;
//this.evaluation.ReviewerSODate == null || this.evaluation.ReviewerSODate == "" ? this.evaluation.ReviewerSODate = Helpers.getCurrentDate() : this.evaluation.ReviewerSODate = "";
this.$emit('reviewer-so-changed', this.evaluation.ReviewerSO, this.evaluation.ReviewerSODate);
},
ManagementSOChanged: function () {
this.managementClicked = true;
//this.evaluation.ManagementSODate == null || this.evaluation.ManagementSODate == "" ? this.evaluation.ManagementSODate = Helpers.getCurrentDate() : this.evaluation.ManagementSODate = "";
this.$emit('management-so-changed', this.evaluation.ManagementSO, this.evaluation.ManagementSODate == null || this.evaluation.ManagementSODate == "" ? Helpers.getCurrentDate() : "");
}
},
computed: {
enableEmployeeSO: function () {
return (this.perspective == "Employee" && !this.evaluation.EmployeeSO) || this.employeeClicked;
},
enableReviewerSO: function () {
return (this.perspective == "Reviewer" && !this.evaluation.ReviewerSO && this.evaluation.EmployeeSO) || this.reviewerClicked;
},
enableManagementSO: function () {
return (this.perspective == "Management" && !this.evaluation.ManagementSO && this.evaluation.ReviewerSO && this.evaluation.EmployeeSO) || this.managementClicked;
},
employeeSODate: function () {
return this.evaluation.EmployeeSODate != null && this.evaluation.EmployeeSODate == new Date("01-01-1900") ? "" : this.evaluation.EmployeeSODate != null && this.evaluation.EmployeeSODate.length >= 10 ? this.evaluation.EmployeeSODate.substring(0, 10) : this.evaluation.EmployeeSODate;
},
reviewerSODate: function () {
return this.evaluation.ReviewerSODate != null && this.evaluation.ReviewerSODate == new Date("01-01-1900") ? "" : this.evaluation.ReviewerSODate != null && this.evaluation.ReviewerSODate.length >= 10 ? this.evaluation.ReviewerSODate.substring(0, 10) : this.evaluation.ReviewerSODate;
},
managementSODate: function () {
return this.evaluation.ManagementSODate != null && this.evaluation.ManagementSODate == new Date("01-01-1900") ? "" : this.evaluation.ManagementSODate != null && this.evaluation.ManagementSODate.length >= 10 ? this.evaluation.ManagementSODate.substring(0, 10) : this.evaluation.ManagementSODate;
}
}
});
export class Evaluation {
private _EmployeeName: string;
private _EmployeeSO: boolean;
private _EmployeeSODate: Date;
private _ReviewerName: string;
private _ReviewerSO: boolean;
private _ReviewerSODate: Date;
private _ManagementReviewerName: string;
private _ManagementReviewerSO: boolean;
private _ManagementReviewerSODate: Date;
constructor() {
this._EmployeeName = "";
this._EmployeeSO = false;
this._EmployeeSODate = new Date("01-01-1900");
this._ReviewerName = "";
this._ReviewerSO = false;
this._ReviewerSODate = new Date("01-01-1900");
this._ManagementReviewerName = "";
this._ManagementReviewerSO = false;
this._ManagementReviewerSODate = new Date("01-01-1900");
}
get EmployeeName(): string {
return this._EmployeeName;
}
set EmployeeName(employeeName: string) {
if (this._EmployeeName != employeeName) {
this._EmployeeName = employeeName;
}
}
get EmployeeSO(): boolean {
return this._EmployeeSO;
}
set EmployeeSO(employeeSO: boolean) {
if (this._EmployeeSO != employeeSO) {
this._EmployeeSO = employeeSO;
}
}
get EmployeeSODate(): Date {
return this._EmployeeSODate;
}
set EmployeeSODate(employeeSODate: Date) {
if (this._EmployeeSODate != employeeSODate) {
this._EmployeeSODate = employeeSODate;
}
}
get ReviewerName(): string {
return this._ReviewerName;
}
set ReviewerName(reviewerName: string) {
if (this._ReviewerName != reviewerName) {
this._ReviewerName = reviewerName;
}
}
get ReviewerSO(): boolean {
return this._ReviewerSO;
}
set ReviewerSO(reviewerSO: boolean) {
if (this._ReviewerSO != reviewerSO) {
this._ReviewerSO = reviewerSO;
}
}
get ReviewerSODate(): Date {
return this._ReviewerSODate;
}
set ReviewerSODate(reviewerSODate: Date) {
if (this._ReviewerSODate != reviewerSODate) {
this._ReviewerSODate = reviewerSODate;
}
}
get ManagementReviewerName(): string {
return this._ManagementReviewerName;
}
set ManagementReviewerName(managementReviewerName: string) {
if (this._ManagementReviewerName != managementReviewerName) {
this._ManagementReviewerName = managementReviewerName;
}
}
get ManagementReviewerSO(): boolean {
return this._ManagementReviewerSO;
}
set ManagementReviewerSO(managementReviewerSO: boolean) {
if (this._ManagementReviewerSO != managementReviewerSO) {
this._ManagementReviewerSO = managementReviewerSO;
}
}
get ManagementReviewerSODate(): Date {
return this._ManagementReviewerSODate;
}
set ManagementReviewerSODate(managementReviewerSODate: Date) {
if (this._ManagementReviewerSODate != managementReviewerSODate) {
this._ManagementReviewerSODate = managementReviewerSODate;
}
}
}
我刚注意到,在我的孩子组件中,我正在使用MangementSO
&amp; ManagementSODate
,而模型有ManagementReviewerSO
&amp; ManagementReviewerSODate
。更改这些修复了我的代码。但是,基于下面的讨论,我有点困惑为什么将道具放入本地数据是处理这种情况的错误方法。有人可以解释一下吗?
答案 0 :(得分:2)
使用props初始化数据属性没有任何问题。我认为这里很多评论中最大的混淆来源是initEvaluation
是对象。在这种情况下,对该对象的任何更改都将反映在使用该对象的无处不在的。
在相关代码中,父组件中的evaluation
和子组件中的evaluation
是同一个对象。对该对象进行的更改将反映在父对象中,并且对Vue的部分没有 no 投诉,因为您在技术上不会改变对该对象的引用,您只是更改了它的属性的值。
通常,如果您将原语值作为属性传递,Vue将发出警告。假设您传递了一个字符串值,然后将该值与v-model
一起使用。在那个的情况下,Vue会抛出一个警告(在开发版本中)你正在改变一个属性。警告有两个原因;首先是因为该值不传播到父级(这可能是意外行为),其次,因为无论何时在父级中更改数据,它都会覆盖子。
当一个对象或数组作为属性传递时,Vue只会在您更改对象引用时发出警告。例如,在您的代码中,如果您这样做:
this.initEvaluation = new Evaluation()
Vue会抱怨你正在改变一个财产。但是,在您的代码中,您只需更改initEvaluation
的属性,这不会产生警告,并且还会导致这些值反映在无处不在,因为你到处都在更新相同的对象。
其中一个原因是,在您的代码中有效,设置evaluation: this.initEvaluation
是虚假的。您可以在您的模板中使用initEvaluation
,并获得与使用evaluation
相同的结果。同样,这是因为它们是相同的对象。这是Luiz试图解释的一部分。在这种情况下,路易斯的第一句话有点误导。数据函数只被称为一次,这意味着使用属性初始化的数据将只接收属性的值一次。但是,因为initEvaluation
是一个对象,所以在问题的代码中它并不重要。对象引用永远不会更改,只有属性才会更改。如果父母由于某种原因更改引用,则子项中的evaluation
将使用新的引用进行更新。
对于对象的所有地方是否立即反映的更新是理想的是值得商榷的。在许多情况下,您希望在发生更新时控制。在这些情况下,您可能会执行evaluation: Object.assign({}, this.initEvaluation)
之类的操作,从而生成initEvaluation
对象的副本。这样做的优点是,您可以根据需要对子组件中的对象进行任意多次更改,而不会将这些更改反映在外部组件中。然后,在验证所有更改都正确后,您可以发出这些更改。
答案 1 :(得分:1)
当您根据data
(props
和initEvaluation
)创建perspective
属性时,他们不再依赖道具而您只能引用数据,就好像它们只是副本一样,但不是收到的实际道具。所以,他们不会对道具做出反应。
由于您似乎没有在组件中更新它们,因此您可以直接引用props
,而不是data
创建的props
。
编辑:请注意,您在v-model
上使用evaluation
,以便在您直接引用它们时更改道具。
因此,一般来说,我会从数据中删除它:
evaluation: this.initEvaluation
// ...
currentCommentSource: this.perspective
直接引用props
,并按照文档中的建议将v-model
替换为:value
和@input
。