简体   繁体   English

与Vue.js的MVVM绑定无法按预期工作

[英]MVVM binding with Vue.js does not work as expected

I'm currently making a dynamic-sized editable list component in a form. 我目前正在用表格制作一个可动态调整大小的列表组件。

I have at least one input field shown which is responsible for the creation of new fields as you type. 我至少显示了一个输入字段,该字段负责在您键入内容时创建新字段。 If you type anything else than a whitespace character, the value of this field is added to the model and then reseted. 如果键入除空格字符以外的任何字符,则此字段的值将添加到模型中,然后重置。

On the next tick, Vue updates the view and create the new input field with the letter you typed in, and I give the focus to this field so the user can continue typing as-if nothing happened for him. 在下一个刻度上,Vue更新视图并使用您键入的字母创建新的输入字段,我将重点放在该字段上,以便用户可以继续输入,前提是他什么都没发生。

So when the field is created, the model gets a new item which has the letter you typed in as its value. 因此,在创建字段时,模型将获得一个新项目,该项目具有您键入的字母作为其值。 The problem is that when you edit the created field, the model isn't updated. 问题是,当您编辑创建的字段时,不会更新模型。

I made a JSFiddle so you can check it by yourself 我做了一个JSFiddle,所以您可以自己检查

itemBlured: function (idx) {
    console.log(vm.songs[idx].name); // Always print the same letter for a given field
}

The final goal of the itemBlured method is to remove the last edited entry in the model if its value is empty. itemBlured方法的最终目标是删除模型中最后一个编辑的条目(如果其值为空)。 But for now you can see, by opening your dev console that even if you change the value of a field, the Vue model isn't updated. 但是现在,通过打开开发人员控制台,您可以看到,即使您更改字段的值,Vue模型也不会更新。

Any help or idea is welcome :) 任何帮助或想法都欢迎:)

I found the reason why the binding did not occured. 我找到了没有发生绑定的原因。

When dealing with <input> tags, you must use the v-model attribute instead of value to tell Vue.js to bind the input to your model, otherwise it just act as mustache template. 处理<input>标记时,必须使用v-model属性而不是value来告诉Vue.js将输入绑定到模型,否则,它仅用作胡须模板。

Hope it helps someone one day. 希望有一天能对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM