简体   繁体   English

如何更改 vue 元素的顺序

[英]How to change vue elements order

I have this object structure:我有这个对象结构:

lines: [{ 
  order: '1', 
  text: ' blue' 
},{
  order: '2', 
  text: 'green' 
},{ 
  order: '3',   
  text: 'yellow' 
}]

And this is rendered on the page like this:这在页面上呈现如下:

Blue
Green
Yellow

I want reorder the elements (and the object) without drag-drop, but with button up and down.我想重新排序元素(和对象)而不使用拖放,但使用按钮向上和向下。 Like this:像这样:

Blue - [down]
Green [up, down]
Yellow [up]

Each bullet is a component.每个子弹都是一个组件。 How can I achieve that?我怎样才能做到这一点?

Based on assumptions from the little information you provided, I gave it a go.根据您提供的少量信息的假设,我试了一下。

Read: Vuejs list caveats阅读: Vuejs 列表注意事项

From the documentation:从文档中:

Due to limitations in JavaScript, Vue cannot detect the following changes to an array:由于 JavaScript 的限制,Vue 无法检测到数组的以下更改:
When you directly set an item with the index, eg vm.items[indexOfItem] = newValue当您直接设置带有索引的项目时,例如vm.items[indexOfItem] = newValue

So when you modify an array in Vue you should use one of the following:因此,当您在 Vue 中修改数组时,您应该使用以下方法之一:

// Vue.set
Vue.set(example1.items, indexOfItem, newValue)
// Array.prototype.splice
example1.items.splice(indexOfItem, 1, newValue)

Your view can look something like:您的视图可能类似于:

 <div id="app"> <div v-for="(line, index) in lines"> <p style="display: inline-block">{{line.text}}</p> <button @click="up(index)" v-if="index !== 0">UP</button> <button @click="down(index)" v-if="index !== lines.length-1">DOWN</button> </div> </div>

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

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