【问题标题】:Error with v-bind in vue.jsvue.js 中的 v-bind 错误
【发布时间】:2018-11-23 09:58:14
【问题描述】:

我尝试使用 axios 从我的 API 服务器获取数据,但出现此错误:

'v-bind' directives require an attribute value.

我不知道我必须如何使用v-bind 来修复它。 这是我的代码:

<template>
  <div class="main">
    <p class="title"><span>Fetured Items</span><br>
    <span>Shop for items based on what we featured in this week</span></p>
    <div v-if="products && products.length" class="content">
      <content-item v-for="product in products" :key="product.id"
      v-bind:name="product.name"
      v-bind:price="product.price"
       v-bind:srcToProdImage= 'localhost:8081/' + product.productImage></content-item>
    </div>
    <p class="cont-btn">
      <button class="btn">Browse All Product <span><i class="fas fa-arrow-right"></i></span></button>
    </p>
  </div>
</template>

<script>
import Content_item from './Content-item';
import axios from 'axios';

export default {
  data:{
    products,
    errors
  },
  created(){
    axios.get('localhost:8081/products')
    .then((result) => {
      this.products = result.data.products
    }).catch((err) => {
      this.errors.push(err)
    });
  },
  components: {
    'content-item' : Content_item
  }
}
</script>

<style lang="scss" scoped>
.content{
  display: flex;
  flex-wrap: wrap;
  margin-left: 150px;
  margin-right: 150px;
  justify-content: space-between;
}
.title{
  text-align: center;
  margin-top: 40px;
  margin-bottom: 30px;
  span:first-child{
    font-family: 'Lato', sans-serif;
    font-weight: 800;
    color: #222222;
    font-size: 30px;
  }
  span:last-child{
    font-family: 'Lato', sans-serif;
    font-weight: 400;
    color: #9f9f9f;
    font-size: 14px;
  }
}
.cont-btn{
  display: flex;
  justify-content: center;
}
.btn{
  padding: 18px 24px 18px 24px;
  background-color: #f16d7f;
  font-family: 'Lato', sans-serif;
  font-weight: 800;
  border: none;
  outline: none;
  color: #ffffff;
}
</style>

错误:

【问题讨论】:

  • 这个v-bind:srcToProdImage= 'localhost:8081/' + product.productImage 无效。
  • 您可以使用返回预期 url 的计算值并将计算值用作绑定值。

标签: javascript vue.js axios


【解决方案1】:

只需在 srcToProntImage 表达式周围加上双引号即可:

<content-item v-for="product in products" :key="product.id"
      v-bind:name="product.name"
      v-bind:price="product.price"
       v-bind:srcToProdImage="'localhost:8081/' + product.productImage"></content-item>

【讨论】:

  • 谢谢。但我得到另一个错误。我更新问题。
  • 一点解释 - vue 正在尝试评估分配给 v-bind:whatever 的表达式,因此您需要在字符串周围加上引号,因为 javascript 中的 localhost:8081/ + someVar 也会引发异常。
  • 需要更新数据函数:products: [], errors: []
【解决方案2】:

您可能有一些语法错误。请检查您的 Vue 相关代码中是否有一些 或 exstra " 字符。这是我的错误。

【讨论】:

    猜你喜欢
    • 2017-07-04
    • 2018-05-15
    • 2018-12-20
    • 2017-06-12
    • 2016-06-10
    • 2020-07-23
    • 2019-06-21
    • 1970-01-01
    • 2019-02-08
    相关资源
    最近更新 更多