我正在编写一个小型Vue应用程序。我有一个元素,它有一个数据范围属性,如下所示:
:data-range="form.appearence.height_min + '/7'"
form.appearence.height_min
将根据用户选择的select元素值进行更改。
每次选择更改后,我将再次读取数据范围,并在此基础上进行操作。
// from the vue app, a watcher
'form.appearence.xps':function(val, oldval){
// this will properly change the model and the dom as well
this.$set(this.form.appearence, 'height_min', xps_map[val]);
this.$emit('xps-updated');
}
// then from another script
this.options.vue.$on('xps-updated', function(){
this.options.vue.$nextTick(function(){
console.log($('#test5').data('range')) // issue: this value doesn't change
}.bind(this))
}.bind(this));
我的问题是范围值在dom上确实会改变,我可以从控制台看到它,但是javascript总是读取初始值.例如,开始时是3/7,然后改为5/7,但是$('#test5').data('range')
仍然会读3/7。为什么?
发布于 2022-08-25 19:37:32
好的,我自己回答。我发现jquery对象不遵循Vue的dom更新,至少在本例中是这样的。因此,即使dom被Vue更新,$('#test5').data('range')
也总是给出初始值。
相反,通过使用香草js获取“真实”元素,如
let range = document.querySelector('#test5').dataset.range;
将始终返回更新的值。
https://stackoverflow.com/questions/73491094
复制相似问题