vue.js和Laravel通过JSON数据

发布于 2025-01-30 19:58:23 字数 2159 浏览 0 评论 0原文

我一直在构建一个简单的表格,该表从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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2025-02-06 19:58:23

您正在使用/home url作为数据终点和HTML端点。您应该做的是设计类似的内容:

Route: GET /api/home
Returns: JSON data
Code: `return response()->json(...);`

Route:: GET /home
Returns: HTML data with javascript that requests /api/home
Code: `return view('home');`

这两种路线都是普通的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:

Route: GET /api/home
Returns: JSON data
Code: `return response()->json(...);`

Route:: GET /home
Returns: HTML data with javascript that requests /api/home
Code: `return view('home');`

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文