【问题标题】:How do I display data through components with Vue.js (using Vueify)?如何使用 Vue.js(使用 Vueify)通过组件显示数据?
【发布时间】:2026-02-05 10:00:02
【问题描述】:

我无法让数据显示在我的 Vue 组件中。我正在使用 Vueify,我正在尝试从 listings.vue 组件加载列表数组,但我不断收到错误消息。另外,我不明白如何通过computed 方法提取数据。任何帮助将不胜感激。

这是我在控制台中遇到的错误:

[Vue warn]: The "data" option should be a function that returns a per-instance value in component definitions. 
[Vue warn]: $mount() should be called only once.

这是我的 app.vue

// app.vue
<style>
  .red {
    color: #f00;
  }
</style>

<template>
    <div class="container">
        <div class="listings" v-component="listings" v-repeat="listing"></div>
    </div>
</template>

<script>
    module.exports = {
        replace: true,
        el: '#app',
        components: {
            'listings': require('./components/listings.vue')
        }
    }
</script>

这是我的 Listings.vue 组件

<style>
.red {
  color: #f00;
}
</style>

<template>
  <div class="listing">{{title}} <br> {{description}}</div>
</template>

<script>

    module.exports = {

          data: {
            listing: [
              {
                title: 'Listing title number one',
                description: 'Description 1'
              },
              {
                title: 'Listing title number two',
                description: 'Description 2'
              }
            ]
          },

        // computed: {
        //  get: function () {
        //      var request = require('superagent');
        //      request
        //      .get('/post')
        //      .end(function (res) {
        //          // Return this to the data object above
      //                // return res.title + res.description (for each one)
        //      });
        //  }
        // }
    }
</script>

【问题讨论】:

    标签: javascript arrays node.js vue.js


    【解决方案1】:

    第一个警告意味着当您定义组件时,data 选项应如下所示:

    module.exports = {
      data: function () {
        return {
          listing: [
              {
                title: 'Listing title number one',
                description: 'Description 1'
              },
              {
                title: 'Listing title number two',
                description: 'Description 2'
              }
            ]
         }
       }
    }
    

    另外,不要将 ajax 请求放在计算属性中,因为每次访问该值时都会计算计算 getter。

    【讨论】:

    • 为什么是@Evan You?文档还提到了the same thing
    • 为什么数据必须包装在一个函数中,而道具却不必?