【发布时间】:2019-11-20 18:09:58
【问题描述】:
Axios 加载数据没有任何问题,不显示任何数据,Laravel Mix 构建没有错误。
我有以下代码:
index.html(正文)
<div id="app">
<posts></posts>
</div>
在 app.js 我使用这个:
import Vue from 'vue';
// global declare axios
window.axios = require('axios');
import Posts from './components/Posts';
Vue.component('posts', Posts);
new Vue({
el: '#app',
props: {
posts:[{
userId: [Number],
id: [Number],
title: [String, Number]
}]
}
});
在 Posts.vue 组件中,我创建了一个模板和一个在安装时加载数据的脚本:
<template>
<ul>
<li v-for="post in posts" v-text="post.title"></li>
</ul>
</template>
<script>
export default {
data: function() {
return {
posts: null,
}
},
// when stuff is loaded (document ready)
mounted: function() {
// use placeholder data for tests, capture asynchronous data from site using this.
axios.get('https://jsonplaceholder.typicode.com/posts')
.then(response => this.posts = response.posts)
.catch(error => this.posts = [{title: 'No posts found.'}])
.finally(console.log('Posts loading complete'));
},
}
</script>
所以数据应该显示为一个ul列表:
<ul>
<li>
title
</li>
<li>
title
</li>
<li>
title
</li>
</ul>
【问题讨论】:
标签: laravel vue.js components