我试图将一个数组复制到另一个数组并使用它像新数组一样,不需要对旧数组进行任何更改:
<div id="app">
<div class="form-group">
<label>Test input</label>
<input v-model="testArray[0].name" type="text" class="form-control" placeholder="Input">
</div>
<br>
<pre>testArray: {{ testArray[0] | json}}</pre>
<pre>templateArray: {{ templateArray[0] | json }}</pre>
Run Code Online (Sandbox Code Playgroud)
new Vue({
el: '#app',
data: {
testArray: [],
templateArray: [{name: "TEST"},],
},
ready: function() {
this.testArray = this.templateArray.slice(0);
},
});
Run Code Online (Sandbox Code Playgroud)
问题是我正在更新新数组'testArray'我也改变旧数组'templateArray'.
实施中的脚本:https://jsfiddle.net/4po1cpkp/7/
有没有办法基于数组模板创建新数组而不直接将其绑定到模板?