【问题标题】:create a search bar jsvue from json object从 json 对象创建一个搜索栏 jsvue
【发布时间】:2018-09-07 13:32:24
【问题描述】:

我想创建一个搜索栏,通过 json 对象的数据进行搜索并将数据显示给用户。我目前的代码看起来像这样,并且运行良好。

<html>


<head>
    <script src="https://unpkg.com/vue"></script>
     <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head>   <body> <div id='app'>   <input type='text' v-model='keyword' placeholder='search title'>   <button v-on:click="">automotive</button>   <div v-for="post in filteredList"> <iframe width="420" height="315" v-bind:src="post.link">   </iframe>  <a v-bind:href="post.link">{{post.title}}</a>   </div>   </div>
      <script> "use strict";

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Post = function Post(title, link, author, img) {  
_classCallCheck(this, Post);

  this.title = title;   this.link = link;   this.author = author;   this.img = img; }; var app = new Vue({   el: '#app',   data: {
    keyword:'',
    postList: [
      new Post(
        'Vue.js', 
        'https://www.youtube.com/embed/tgbNymZ7vqY', 
        'Chris', 
        'https://vuejs.org//images/logo.png'
      ),
      new Post(
        'React.js', 
        'https://www.youtube.com/embed/k3frK9-OiQ0', 
        'Tim',
        'http://daynin.github.io/clojurescript-presentation/img/react-logo.png'
      ),
      new Post(
        'Angular.js', 
        'https://angularjs.org/', 
        'Sam', 
        'https://angularjs.org/img/ng-logo.png',
      ),
      new Post(
        'Ember.js', 
        'http://emberjs.com/', 
        'Rachel',
        'http://www.gravatar.com/avatar/0cf15665a9146ba852bf042b0652780a?s=200'
      ),
      new Post(
        'Meteor.js', 
        'https://www.meteor.com/', 
        'Chris', 
        'http://hacktivist.in/introduction-to-nodejs-mongodb-meteor/img/meteor.png'
      ),
      new Post(
        'Aurelia', 
        'http://aurelia.io/', 
        'Tim',
        'https://cdn.auth0.com/blog/aurelia-logo.png'
      ),
      new Post(
        'Node.js', 
        'https://nodejs.org/en/', 
        'A. A. Ron',
        'https://code-maven.com/img/node.png'
      ),
      new Post(
        'Pusher', 
        'https://pusher.com/', 
        'Alex', 
        'https://avatars1.githubusercontent.com/u/739550?v=3&s=400'
      ),
      new Post(
        'Feathers.js', 
        'http://feathersjs.com/', 
        'Chuck',
        'https://cdn.worldvectorlogo.com/logos/feathersjs.svg'
      ), ]   },   methods: {
       },   computed:{
    filteredList(){
      return this.postList.filter((post) => {
        return post.title.toLowerCase().includes(this.keyword.toLowerCase());
      });
    }   } })
      </script>   </body>   <html>

忽略链接的内容无关紧要。然而,我遇到的问题是通过 axios 请求从外部来源使其工作。我之前已经完成了 axios 请求并取回了 json 数据,但我正在努力让这个搜索功能与它一起工作。以下是损坏代码的示例(忽略 v-on:click 它尚未设置。忽略没有视频我可以处理的事实,稍后我只需要搜索功能来处理来自axios 请求),但我不断收到诸如“类型错误:this.item 未定义”和“对象错误”之类的错误代码:

<html>


 <head>
    <script src="https://unpkg.com/vue"></script>
     <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  </head>
  <body>
<div id='app'>
  <input type='text' v-model='keyword' placeholder='search item'>
  <button v-on:click="">automotive</button>
  <div v-for="item in filteredList">

<p>{{item.name}}</p>
  </div>
  </div>
      <script>

var app = new Vue({
  el: '#app',
  data: {
    keyword:'',
    itemList: '',

  },
             created: function() {
            this.loaddata();
            },
            methods: {
                loaddata: function(){
                  var vueapp = this;
                    axios.get('https://jsonplaceholder.typicode.com/users').then(function (response){
                        vueapp.itemList = response.data;
                    })
                },
            },

  computed:{
    filteredList(){
      return this.item.filter((item) => {
        return item.name.toLowerCase().includes(this.keyword.toLowerCase());
      });
    }
  }
})
      </script>
  </body>
  <html>

【问题讨论】:

    标签: json image video vue.js axios


    【解决方案1】:

    您的数据中没有声明 item。当您没有声明 this.item.filter 时,您不能在 filteredList() 方法中调用它。

    您可以将filteredList 更改为this.itemList.filter(),这是在loaddata() 中加载的列表

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2019-06-18
      • 2021-12-24
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多