当前位置:   article > 正文

vue实现置顶_置顶vue

置顶vue

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}
]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

具体代码如下:

<!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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

效果图如下:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/224018
推荐阅读