【发布时间】:2020-04-28 23:12:08
【问题描述】:
我想从我之前使用 VueJS 2 创建的 API 呈现数据。我的 VueJS 代码块不呈现我通过后端服务发送的数据。调试控制台不返回错误。 Firefox 的 Vue Debug 扩展成功返回表数据。但我无法在 HTML 表格中显示数据。代码块来了:
Courses.vue 文件:
<template>
<div class="container">
<h3>Courses:</h3>
<table class="table">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Language</th>
</tr>
</thead>
<tbody>
<tr v-for="course in courses" v-bind:key="course.ID">
<th scope="row">{{course.ID}}</th>
<td>{{course.name}}</td>
<td>{{course.language}}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'Courses',
data() {
return {
courses: null,
};
},
created: function() {
axios
.get('http://localhost:8080/api/index')
.then(res => {
this.courses = res.data;
})
}
}
</script>
<style>
h3 {
margin-bottom: 5%;
}
</style>
App.vue 文件:
<template>
<div id="app">
<Courses />
</div>
</template>
<script>
import Courses from './components/Courses.vue'
export default {
name: 'app',
components: {
Courses
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
我的 API 结果:
// 20200111185700
// http://localhost:8080/api/index
{
"Courses": [
{
"ID": 16,
"Name": "Math",
"Language": "en"
},
{
"ID": 15,
"Name": "Biology",
"Language": "en"
},
}
【问题讨论】:
标签: javascript html vue.js vuejs2 vue-component