Vue实现数据实时模糊搜索效果

Vue实现数据实时模糊搜索效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        [v-cloak] {
            display: none;
        }
    </style>
</head>

<body>
    <div id="app" v-cloak>
        <input type="text" placeholder="搜索新闻" v-model="search">
        <ul>
            <li v-for="item in fiterNews">{{item.title}}</li>
        </ul>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
      let vm =  new Vue({
            el: "#app",
            data: {
                news:[],
                search:""
            },
            created() { 
                axios.get('http://jsonplaceholder.typicode.com/posts').then((res)=>{
                    this.news = res.data
                })
            },
            computed: {
                fiterNews(){
                    var keyword = this.search;
                    return this.news.filter((val)=>{
                         return val.title.match(keyword)
                    })
                }
            },
        });
    </script>
</body>

</html>
暂无评论

请到【后台 - 用户 - 我的个人资料】中填写个人说明。

发表评论