【问题标题】:Vue + Element UI: How to bind data to card component?Vue + Element UI:如何将数据绑定到卡片组件?
【发布时间】:2017-11-02 03:29:17
【问题描述】:

下面是来自 Element-UI 网站的卡片组件示例。我的问题是如何绑定从 API url 接收的数据?

   <el-row :data="hot_project_card"> //data binding -- is this correct?
     <el-col :span="8" v-for="(o, index) in 2" :key="o" :offset="index > 0 ? 2 : 0">
       <el-card :body-style="{ padding: '0px' }">
         <div style="padding: 14px;">
           <span>Yummy hamburger</span>
           <template scope="scope"> {{ scope.project_name }} </template> // code is not working
         </div>
       </el-card>
     </el-col>
   </el-row>

从api接收到的数据是数组类型

export default {
  data() {...}
  return {
    ...
    hot_project_card: {
      fields: [{
        project_name: '',
        project_hot: '',
        ...
      }]
    },
  ...
  }

后端服务器提供的api

    method(): {
      project_card_display () {
        this.$http.get('http://127.0.0.1:8000/api/project_card_display').then((response) => {
          var res = JSON.parse(response.bodyText)
          console.log(res)
          if (res.error_num === 0) {
            this.hot_project_card = res['list'] // data recevied from backend server is saved to hot_project_card
          } else {
            this.$message.error('retrieved error"')
            console.log(res['msg'])
          }
        })
      }
   }

【问题讨论】:

    标签: vue.js vue-resource


    【解决方案1】:

    如果我没看错,您不必将数据传递给el-row 元素。您可以简单地使用数据属性hot_project_card 中的内容:

    <el-row>
        <el-col :span="8" v-for="(o, index) in 2" :key="o" :offset="index > 0 ? 2 : 0">
            <el-card :body-style="{ padding: '0px' }">
                <div style="padding: 14px;">
                    <div v-for="field in hot_project_card.fields">
                        <h4>{{ field.project_name }}</h4>
                        <p>{{ field.project_hot }}</p>
                        ...
                    </div>
                </div>
            </el-card>
        </el-col>
    </el-row>
    

    HTH,干杯!!

    【讨论】:

    • 是否可以动态加载所有字段而不是对每个字段进行硬编码(field.project_name...)?
    猜你喜欢
    • 2022-06-25
    • 2021-09-04
    • 2019-11-13
    • 2020-05-18
    • 2018-07-25
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多