赞
踩
new.json 数据代码:
[
{"id": "G11111","title": "手机","price": 2999},
{"id": "G22222","title": "平板电脑","price": 1999},
{"id": "G33333","title": "笔记本电脑","price": 5999},
{"id": "G44444","title": "树莓派电脑","price": 999},
{"id": "G55555","title": "超级计算机","price": 9999999}
]
具体代码如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script> </head> <body> <div id="app"> <ul > <li v-for="(pr,index) in info" :key="index">{{ pr.id }} {{pr.title}} ¥{{pr.price}} <button @click="aaa(pr,index)">{{hello}}</button> </li> </ul> </div> <script type = "text/javascript"> new Vue({ el: '#app', data () { return { info: null, hello:"置顶", new:"new.json" } }, mounted () { axios.get(this.new) .then(response => { this.info = response.data; console.log(response.data); for (let i=0;i<this.info.length;i++){ console.log(this.info[i].title); if (this.info[i].id == "G33333" || this.info[i].id == "G44444"){ this.info.unshift(this.info[i]); //把查找到的数据复制添加到数组的首位 this.info.splice(i+1,1); //数据复制添加到数组的首位后,原本查找的数据位置会发生改变,所以要往后加一位(i+1),然后删除它 } } }) .catch(error=> { console.log(error); console.log("%c404:"+this.new+"数据请求失败!!!", "color:red;font-size:30px;"); }) }, methods:{ aaa:function (pr,index) { this.info.splice(index,1); this.info.unshift(pr); } } }) </script> </body> </html>
效果图如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。