[英]Updating array inside vue components data
I have a key inside my components data that may be initialised as null or have some strings inside.我的组件数据中有一个键,它可能被初始化为 null 或里面有一些字符串。 I want to be able to create as many associatedKeys as I want, while also allowing for it to be initialised as null or with multiple values.我希望能够创建尽可能多的关联键,同时还允许将其初始化为 null 或具有多个值。 The problem I'm having is that each time I am pressing the button to add a new input field, the page is re rendering and the data is then reset once initialised again.我遇到的问题是,每次我按下按钮添加一个新的输入字段时,页面都会重新呈现,然后数据会在再次初始化后重置。
I have been looking at this article, I have placed a debugger inside the addKeys function and am getting the error message this.licence.associatedKeys.$set is not a function
.我一直在看这篇文章,我在 addKeys 函数中放置了一个调试器,并收到错误消息this.licence.associatedKeys.$set is not a function
。 I don't understand this error and am not sure how to add elements to the associatedKeys array我不明白这个错误,也不确定如何将元素添加到 associatedKeys 数组
<template>
<div>
<form>
<label>Associated Keys</label>
<button v-on:click="addNewKey">Add new key</button>
<div v-for="(k,index) in licence.associatedKeys" :key="k">
<input type="text" :value="k" @input="licence.associatedKeys[index]=$event.target.value">
</div>
</form>
</div>
</template>
<script>
import util from '~/assets/js/util'
export default {
methods: {
addNewKey() {
this.licence.associatedKeys.$set(this.licence.associatedKeys, null)
}
},
data: () => ({
licence: {
associatedKeys: []
}
})
}
</script>
Have a look at event modifiers , specifically this example:看看事件修饰符,特别是这个例子:
<!-- the submit event will no longer reload the page -->
<form v-on:submit.prevent="onSubmit"></form>
I think this is what you need to stop the page reloading.我认为这是您停止页面重新加载所需要的。
As for the undefined error: you can try to use the instance itself, ie至于未定义的错误:您可以尝试使用实例本身,即
this.$set('license.associatedKeys[' + this.license.associatedKeys.length + ']', null);
Also, you probably misread the documentation, the .$set
and .$add
methods on nested data properties take key and value arguments.此外,您可能误读了文档,嵌套数据属性上的.$set
和.$add
方法采用键和值参数。 So, you should have written所以,你应该写
this.licence.associatedKeys.$set(this.licence.associatedKeys.length, null)
the reason you get a redirect is that you have a button inside a form.您获得重定向的原因是您在表单中有一个按钮。 While other browsers don't, Chrome will treat it as a redirect by default.虽然其他浏览器不会,但 Chrome 默认会将其视为重定向。 The easiest way to resolve it is to define an action action="#"
that way, you don't have to handle every button to prevent default action.解决它的最简单方法是定义一个 action action="#"
这样您就不必处理每个按钮来防止默认操作。
@input
is fine, but vue has a lot of built-in functionality, such as v-model
that will automatically bind value, showing and updating it on change. @input
很好,但是 vue 有很多内置功能,例如v-model
会自动绑定值,在更改时显示和更新它。
you don't need to use $set
when you're pushing (plus you set it on the vue instance and not the value ( this.$set(this.myVal, 'myKey', null)
instead of this.myVal.myKey.$set(null)
)您在推送时不需要使用$set
(加上您在 vue 实例上设置它而不是值( this.$set(this.myVal, 'myKey', null)
而不是this.myVal.myKey.$set(null)
)
finally, if you want key-value pair stored in an array, you need two objects the key and the value最后,如果要将键值对存储在数组中,则需要两个对象,键和值
new Vue({ el: "#app", methods: { addNewKey() { //this.licence.associatedKeys.$set(this.licence.associatedKeys, null) this.licence.associatedKeys.push({key:null, val:null}); } }, data: () => ({ licence: { associatedKeys: [] } }) })
body {background: #20262E;padding: 20px;font-family: Helvetica;} button {padding: 8px 16px;border-radius: 20px;border: none;} #app {background: #fff;border-radius: 4px;padding: 20px;transition: all 0.2s;}
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script> <div id="app"> <div> <form action="#"> <label>Associated Keys</label> <button v-on:click="addNewKey">Add new key</button> <div v-for="(k,index) in licence.associatedKeys" :key="k"> <input type="text" v-model="k.key"> <input type="text" v-model="k.val"> </div> </form> </div> <pre>{{this.licence}}</pre> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.