简体   繁体   English

将数据从json渲染到vue html模板

[英]Render data from json to vue html template

I am trying to rendering data to an un-ordered list in Vue using an axios get request.我正在尝试使用 axios get 请求将数据渲染到 Vue 中的无序列表。 The get request function to my mongoDB database works as intended and I am to retrieve the information that I want.我的 mongoDB 数据库的 get 请求函数按预期工作,我将检索我想要的信息。 The issue that the information does not render to the template.信息不呈现给模板的问题。

<template>
  <div class="reviews">
    <ul v-for="review in reviewData" class="list-unstyled" :key="review._id">
       <b-media tag="li">
        <h3>{{review.name}}</h3>
        <p>{{review.content}}</p>
      </b-media>
    </ul>    
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "PostedReview",
  props: ["reviews"],
  
  mounted () {
    axios.get(`http://localhost:4000/reviews`).then((data) => {        
      const reviews = data.data        
      const reviewData = reviews.filter(review => review.beerId === this.$route.params.beerId)        
      console.log(reviewData)
    })      
  }, 

  
  data() {
    return {
      reviewData: []
    }
  },
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

Assign value to the component scoped variable of your component.为组件的组件范围变量赋值。 You were creating a local variable and assigning a value to it, which will not reflect in your component scoped variable which is accessed in template.您正在创建一个局部变量并为其分配一个值,这不会反映在模板中访问的组件范围变量中。

<script>
    import axios from "axios";
    export default {
      name: "PostedReview",
      props: ["reviews"],
      mounted () {
        axios.get(`http://localhost:4000/reviews`).then((data) => {        
          const reviews = data.data        
          this.reviewData = reviews.filter(review => review.beerId === this.$route.params.beerId)        
          console.log(this.reviewData)
        })      
      }, 
    
      
      data() {
        return {
          reviewData: []
        }
      },
    }
    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM