【问题标题】:why items are not loading in blog.vue?为什么没有在 blog.vue 中加载项目?
【发布时间】:2017-05-08 22:59:26
【问题描述】:

我正在 app.js 中从 blog.vue 加载模板,但是由于没有进行 ajax 调用,因此它只显示表的标题行而不是表行。

如果我注释require('./bootstrap');,它不显示表头,调用ajax但表行中不显示数据。

1. blog.vue 文件

<template>
<div class="table-responsive">
  <table class="table table-borderless">
    <tr>
      <th>Title</th>
      <th>Description</th>
    </tr>
    <tr v-for="item in items">
      <td>@{{ item.title }}</td>
      <td>@{{ item.description }}</td>

    </tr>
  </table>
</div>
</template>

<script>
    export default {
        mounted() {
            console.log('Component ready.')
        }
    }
</script>

2。 app.js 文件

require('./bootstrap');

Vue.component('list-blog', require('./components/blog.vue'));

new Vue({
  el :'#manage-vue',
  data :{
    items: [],
    newItem : {'title':'','description':''},
    fillItem : {'title':'','description':'','id':''}
  },

  ready: function() {
    this.getVueItems();
  },
  methods: {
    getVueItems: function() {
      this.$http.get('/vueitems?page='+page).then((response) => {
        this.$set('items', response.data.data.data);
        this.$set('pagination', response.data.pagination);
      });
    },
  }
});

3. bootstrap.js

window._ = require('lodash');

window.$ = window.jQuery = require('jquery');
require('bootstrap-sass');


window.Vue = require('vue');
require('vue-resource');

Vue.http.interceptors.push((request, next) => {
    request.headers.set('X-CSRF-TOKEN', Laravel.csrfToken);

    next();
});

你能告诉我我哪里错了吗?我在这里使用 laravel-elixir。

【问题讨论】:

    标签: laravel vue.js vue-component


    【解决方案1】:

    我认为这应该只是 response.data.data

    // this.$set('items', response.data.data.data);
    // to
    this.$set('items', response.data.data);
    

    你甚至可以简化这个

    this.$http.get('/vueitems?page='+page).then((response) => {
        this.items = response.data.data;
        this.pagination = response.data.pagination;
    }.bind(this)); // important bind(this)
    

    您似乎正在返回一个分页对象。因此第三个数据是错误的——不是吗? 此外,您通常会将项目作为属性传递给组件。只需在你的 blog.vue 中定义 prop

    <script>
    export default {
        props: ['items'],
        mounted() {
            console.log('Component ready.')
        }
    }
    </script>
    

    然后将它传递到任何你使用它的地方

    <list-blog :items="items"></list-blog>
    

    我强烈推荐使用 Chrome 和 Vue devtools https://github.com/vuejs/vue-devtools - 这些将帮助您了解和理解数据绑定

    另一个较小的事情是你不应该单独绑定分页对象和项目。分页对象包含项目。如果您不想在模板中使用“pagination.data”,只需使用计算属性,而不是创建两个事实来源。

    【讨论】:

    • 但是为什么 vue 不进行 api 调用?而且我使用了分页,但在这里我省略了,所以我想ajax调用没有问题。
    • 尝试使用“created”而不是ready,还请告诉我们您正在使用哪个vue js版本
    • 我尝试过“创建”而不是准备好,但对我不起作用。而且我尝试过使用 vue js 1.0.26 和 2.1.6,但没有成功。
    • 您没有收到任何控制台错误?你需要 vue 资源吗?在您的问题中,您说“调用了 ajax,但没有显示数据” - 为什么现在不再调用 ajax?
    • 如果我评论 require('./bootstrap');然后虽然没有显示数据,但会调用 ajax。如果我不评论什么都没有发生。
    猜你喜欢
    • 2023-02-03
    • 1970-01-01
    • 2017-09-28
    • 2020-10-07
    • 2021-11-27
    • 2016-12-14
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多