属性中的动态变量被视为未定义

1797年

我有2个Vue组件。父组件:list of services谁存储许多service组件。

服务视图

<template lang="pug">
  a.list-group-item.list-group-item(id='list-' + name + '-list' data-toggle="list" href="#list-" + name role="tab" aria-controls=name) {{ name }}
</template>

<script>
export default {
  props: {
    name: String,
    description: String
  }
}
</script>

List.vue

<template lang="pug">
  section.border-bottom.border-warning
    .container(style="background-color: #EA846F")
      .text-white.text-center.title Serviciile oferite
    .list-group#list-tab(v-if="response.status === 200" role="tablist")
      service(v-for="service in response.data" :name="service.name" :description="service.description")
    .h5.text-muted.text-center(v-else) {{ response.statusText }}
</template>

<script>
import service from './service';

import axios from 'axios';

export default {
  data() {
    return {
      response: {},
    }
  },
  components: {
    service
  },
  methods: {
    async getAll() {
      this.response = await axios.get('http://localhost:3000/infos');
    }
  },
  async mounted() {
    await this.getAll();
  }

为什么要name作为prop发送并用于服务模板undefined我怎样才能解决这个问题 ?

菲尔

我完全不了解Pug,但似乎您需要使用v-bind语法,就像您要在属性值内插补道具或数据的其他任何地方一样

a.list-group-item.list-group-item(:id="`list${name}-list`" :href="`#list-${name}`" data-toggle="list"  role="tab" aria-controls=name) {{ name }}

参见https://vuejs.org/v2/guide/syntax.html#Using-JavaScript-Expressions

我使用了模板文字,但是您也可以恢复为串联,例如

a(:id="'list' + name + '-list'" ...

我也将至少初始化response一个空data数组,以便您不要尝试undefined在初始显示上进行迭代,即

return {
  response: {
    data: []
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章