【发布时间】:2020-07-02 22:02:08
【问题描述】:
我正在使用 Vue.js 创建患者预约系统的前端,它只是一个动态 Web 表单。用户首先选择treatment类型,然后选择他们想看的practitioner,最后选择appointment时间。所有数据都是通过使用 axios 的 RESTful API 调用获取的。
每个表单域中的可用选项都使用前一个选项进行过滤。例如,用户只会看到他们选择的从业者的可用时间,并且从业者只能从可以执行所选治疗的从业者组中选择。
根据选择的治疗过滤从业者就可以了。
但是,根据所选从业者过滤约会不起作用 - 它不同步:为先前选择的从业者加载约会。我检查了后端,这很好,API 调用是同步的(即person_id 与新选择的从业者的 id 匹配)。
是什么导致了这个问题,我该如何解决?
这是执行此过滤的 Vue.js 代码:
var app = new Vue({
el: '#app',
data: {
appointments: [],
practitionerId: 0,
practitioners: [],
treatmentId: 0,
treatments: [],
},
mounted: function () {
axios.get('/api/treatments')
.then(response => this.treatments = response.data);
},
watch: {
// filter available practitioners by the selected treatment
treatmentId: function () {
// get the allowed role ids for the selected treatment
var allowedRoleIds = '';
const allowedRoles = this.treatments[this.treatmentId - 1]['allowed_roles'];
for (var i = 0; i < allowedRoles.length; i++) {
allowedRoleIds += allowedRoles[i]['id'];
if (i + 1 < allowedRoles.length) {
allowedRoleIds += ',';
}
}
// load the practitioners using the allowed role ids
axios.get('/api/people?role_ids=' + allowedRoleIds)
.then(response => this.practitioners = response.data);
},
// filter the available appointments by the selected practitioner
practitionerId: function () {
axios.get('/api/appointments?person_id=' + this.practitionerId)
// ERROR!!! This is out of sync.
.then(response => this.appointments = response.data);
}
}
});
【问题讨论】:
-
我猜问题出在 'watch' 语句 - 看起来当有人尝试选择约会时它触发了 proitioner 的请求
-
此时无法选择约会(我还没有实现)。观察者应该触发请求。
treatmentId的更改(选择治疗)应触发加载从业者的请求(它有效),practitionerId的更改(选择从业者)应触发加载约会的请求(它没有'不 工作)。这些观察者之间存在对称性,但只有一个失败了,所以我对这种行为感到非常困惑。我错过了一些基本的东西,但我不知道是什么(我对 Vue.js 和 axios 都很陌生)。 -
据我了解与axios无关
-
我是 vue 新手,但我会尝试通过创建带有相关组合的代码框演示来帮助您
标签: javascript rest vue.js axios