我有一个仅使用Rails API的应用程序,该应用程序可以在Story模型上执行基本的功能。 Vue模板应该使用API。这段时间运行良好,但突然似乎停止了。
在Postman中检查API,CRUD功能正在按预期运行。但是,Vue组件出现故障。例如。索引视图会呈现但不显示任何数据,即使“网络”标签显示的响应为200,也包含适当的数据。见下文:
以下是相关的前端文件:
List.vue:
<template>
<div class="stories">
<h1>Stories</h1>
<div v-if="stories" class="table-wrap">
<div>
<router-link v-bind:to="{ name: 'NewStory' }" class="">Add Story</router-link>
</div>
<div v-for="story in stories" :key="story.id">
<p>Title: {{story.id}}</p>
<p>Overview: {{story.attributes.description}}</p>
<button @click="deleteStory(story.id)">Delete {{story.attributes.title}} </button>
</div>
</div>
<div v-else>
There are no stories.. Let's add one now <br><br>
<!-- <router-link v-bind:to="{ name: 'NewStory' }" class="add_story_link">Add Story</router-link> -->
</div>
</div>
</template>
<script>
import StoriesService from '@/services/StoriesService'
export default {
name: 'stories',
data () {
return {
stories: []
}
},
mounted () {
this.getStories()
},
methods: {
async getStories () {
const response = await StoriesService.fetchStories()
this.stories = response.data.data
console.log(this.stories)
},
async deleteStory (id) {
await StoriesService.deleteStory(id)
this.$router.push({ name: 'Stories' })
}
}
}
</script>
App.vue:
<template>
<div id="app">
<router-view/>
</div>
</template>
<script>
export default {
name: 'App'
}
</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>
StoriesService.js:
import Api from '@/services/Api'
export default {
fetchStories () {
return Api().get('stories')
},
addStory (params) {
return Api().post('stories', params)
},
getStory (params) {
return Api().get(`/stories/${params.id}/`)
},
updateStory (params) {
return Api().put(`/stories/${params.id}/`, params)
},
deleteStory (id) {
return Api().delete(`stories/` + id)
}
}
Api.js:
import axios from 'axios'
export default() => {
return axios.create({
baseURL: `http://localhost:3000/v1`
})
}
main.js:
import Vue from 'vue'
import App from './App'
import router from './router'
import VueAxios from 'vue-axios'
import { securedAxiosInstance, plainAxiosInstance } from './backend/axios'
Vue.config.productionTip = false
Vue.use(VueAxios, {
secured: securedAxiosInstance,
plain: plainAxiosInstance
})
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
securedAxiosInstance,
plainAxiosInstance,
components: { App },
template: '<App/>'
})