【问题标题】:How to write a function with data from different jsons?如何使用来自不同 json 的数据编写函数?
【发布时间】:2018-12-14 12:02:05
【问题描述】:

我有这个代码。

<script>
import './styling.scss'
export default {
  name: "app",
  data() {
    return {
      items: {books:[], authors:[]}
    };
  },

  created: function() {
    this.makeAjaxCall("books.json", "get").then(res => {
        this.items.books = res.books;
        return res;
    }),

    this.makeAjaxCall("authors.json", "get").then(res => {
        this.items.authors = res.authors;
        return res;
    })
  },

  methods: {

    makeAjaxCall:function(url, methodType){
      var promiseObj = new Promise(function(resolve, reject){
          var xhr = new XMLHttpRequest();
          xhr.open(methodType, url, true);
          xhr.send();
          xhr.onreadystatechange = function(){
          if (xhr.readyState === 4){
            if (xhr.status === 200){
                //alert("xhr done ok");
                var response = xhr.responseText;
                var respJson = JSON.parse(response);
                resolve(respJson);
            } else {
                reject(xhr.status);
               //alert("xhr failed");
            }
          } else {
            //alert("xhr processing");
          }
      }
      //alert("request sent succesfully");
    });
    return promiseObj;
    },

    returnAuthorName:function(authorId){
      for(i in items.books)
      _.find(items.authors, { 'id': items.books.authors });
      return items.authors.name
    }
  }
};
</script>
<template>
  <div id="app">
    {{items.authors}}
    <table class="booksTable">
      <thead>
        <tr>
          <th>Title</th>
          <th>Author</th>
          <th>Genre</th>
          <th>Image</th>
          <th>Availability</th>
          <th>Options</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in items.books" :key="item.name">
              <td>{{item.name}}</td>
              <td>{{items.authors.name}}</td>
              <td>{{item.genre}}</td>
              <td><img id="imageBook" :src="item.imageUrl"></td>
              <td v-if="item.availability">Available</td>
              <td v-else>Unavailable</td>
              <td>
                <button class="btn add"> Add</button>
                <button class="btn edit"> Edit</button>
                <button class="btn delete"> Delete</button>
              </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

我想编写一个函数,它接收作者的 id 作为参数(来自 books.json),并返回作者的姓名(来自 authors.json) 我不知道如何编写函数或如何/在哪里使用它。 我在vue中工作,所以它让一切都变得有点不清楚。 有人可以帮忙吗?

【问题讨论】:

    标签: json function vue.js


    【解决方案1】:

    我会在这里使用computed property。因此,请按原样进行调用并删除 returnAuthorName 函数。

    这些工作的方式,是随着items的更新,这个方法将被调用并返回一些东西。

    computed: {
      booksWithAuthor () {
        const { books, authors } = this.items
    
        // If we don't have both of these don't do anything...
        if (books.length === 0 || authors.length === 0) {
          return []
        }
    
        return books.map(book => ({
          ...book,
          author: authors.find(author => author.id === book.authorId),
        }))
      },
    }
    

    然后在您的模板中您将拥有:book.author.name,只需循环通过 booksWithAuthor 而不是 items.books

    【讨论】:

    • 这是一个传播运算符。这有点像复制所有对象属性和值并将它们放入一个新对象中......
    • 不确定应该显示什么而不是 {{items.authors.name}}
    • 我的电脑死机了。对不起!我稍后会更新代码,但应该很接近。
    • 非常感谢
    • 我在这里做了一个简单的例子,因为我担心我们会在其他地方被绊倒。这应该为您提供完成此操作所需的大部分内容:jsfiddle.net/crswll/p3zg7rwd/9
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2015-09-02
    • 2021-09-11
    • 2021-10-16
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    相关资源
    最近更新 更多