如何使用Vue.js(使用Vueify)通过组件显示数据?

par*_*gee 11 javascript arrays node.js vue.js

我无法在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.
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这是我的listing.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>
Run Code Online (Sandbox Code Playgroud)

Eva*_*You 36

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

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

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

  • 为什么@Evan You?文档还提到[同一件事](https://vuejs.org/v2/guide/components.html#data-Must-Be-a-Function) (3认同)