vue.js和Laravel通过JSON数据
我一直在构建一个简单的表格,该表从MongoDB数据库中获取数据库,该数据库正在查询JSON数据,而不是需要将数据传递给前端上的VUE组件,但我无法理解JSON数据如何从拉拉维尔转移到vue。在此处了解代码是我到目前为止所做的:
api.php路由:
Route::middleware('api')->group(function () {
Route::resource('/home', [ProductController::class,'home']);
});
web.php路由:
Route::get('/home', [PostController::class,'home']);
PostController Home方法:
public function home() {
$posts = Post::paginate(4);
return response()->json($posts);
}
HOME组件:
<template>
<div>
<table>
<tr>
<td>Id</td>
<td>Title</td>
<td>Status</td>
</tr>
<tr v-for="El in results" v-bind:key="El.id" >
<td>{{El.id}}</td>
<td>{{El.STATUS}}</td>
</tr>
</table>
</div>
</template>
<script>
export default{
data() {
return {
results: []
};
},
methods: {
fetch() {
axios.get('/api/home')
.then(response => this.results = response.data)
.catch(error => console.log(error));
}
}
}
</script>
Home.blade.php@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Data</div>
<div class="card-body">
<home-component></home-component>
</div>
</div>
</div>
</div>
</div>
@endsection
所有这些似乎都很好,但是当我运行代码时,它只是显示JSON,并且不会呈现任何VUE组件。任何帮助都将受到赞赏。
I have been working on building a simple table that gets its data from a mongodb database that is being queried for the json data and not the data needs to be passed to the vue component on the frontend but I am unable to understand how the json data is transferred from laravel to vue. To understand the code here is what I have done so far:
api.php route:
Route::middleware('api')->group(function () {
Route::resource('/home', [ProductController::class,'home']);
});
web.php route:
Route::get('/home', [PostController::class,'home']);
PostController Home method:
public function home() {
$posts = Post::paginate(4);
return response()->json($posts);
}
Home component:
<template>
<div>
<table>
<tr>
<td>Id</td>
<td>Title</td>
<td>Status</td>
</tr>
<tr v-for="El in results" v-bind:key="El.id" >
<td>{{El.id}}</td>
<td>{{El.STATUS}}</td>
</tr>
</table>
</div>
</template>
<script>
export default{
data() {
return {
results: []
};
},
methods: {
fetch() {
axios.get('/api/home')
.then(response => this.results = response.data)
.catch(error => console.log(error));
}
}
}
</script>
Home.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Data</div>
<div class="card-body">
<home-component></home-component>
</div>
</div>
</div>
</div>
</div>
@endsection
All of this seems fine but when I run the code it just show the json and does not render any of the vue component. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
/home
url作为数据终点和HTML端点。您应该做的是设计类似的内容:这两种路线都是普通的HTTP请求,但是一个仅提供JSON数据,另一个路由使用浏览器来解释HTML数据。
You are using the
/home
URL both as a data endpoint and an HTML endpoint. What you should do is design it something like this:Both routes are ordinary http requests, but one only serves JSON data and the other one serves HTML data to be interpreted by your browser.