【发布时间】:2021-08-11 00:19:03
【问题描述】:
我有一个 Vue 组件,它使用 Laravel Api 数据在模板中显示。我有一个 CustomerController,它使用 index() 方法返回用于创建 API 的 CustomerResource 的集合。我也需要该方法来显示视图。当 CustomerController 的 index 方法用于 API 时,如何显示 index.blade.php。如果您对此问题有任何疑问,请在评论中问我。
客户控制器
public function index() {
return CustomerResource::collection(Customer::all());
}
api.php
Route::middleware('api')->prefix('v1')->group(function() {
Route::apiResource('/customers', CustomerController::class);
});
index.blade.php
@extends('layouts.app')
@section('content')
<all-customers></all-customers>
@endsection
AllCustomers.vue
<template>
<div class="customers">
<div class="main-panel">
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-xl-12 text-right">
<a href="#">
<!-- <router-link class="btn" :to="{ name: 'AddCustomer'}">Add New</router-link > -->
</a>
<a href="#">
<button class="btn">Import</button>
</a>
<!-- <downloadExcel class = "btn btn-default"
:data = "json_data"
:fields = "json_fields"
worksheet = "My Worksheet"
name = "customers.xls">
Export
</downloadExcel> -->
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header card-header-primary">
<h4 class="card-title ">All Customers</h4>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead class=" text-primary">
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th>Actions</th>
</thead>
<tbody>
<tr v-for="(customer,index) in customers" :key="index">
<td>
{{ customer.id }}
</td>
<td>
{{ customer.name }}
</td>
<td>
{{ customer.email }}
</td>
<td>
{{ customer.phone }}
</td>
<td>
{{ customer.address }}
</td>
<!-- <td>
<router-link :to="{name: 'EditCustomer'}">
<span class="material-icons">edit</span>
</router-link>
<span class="material-icons">delete</span>
</td> -->
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import JsonExcel from 'vue-json-excel'
export default {
name:'AllCustomers',
components:{
downloadExcel: JsonExcel
},
data(){
return{
customers:[],
}
},
mounted(){
axios
.get('http://localhost:8000/api/v1/customers')
.then(response=> this.customers = response.data.data)
}
}
</script>
【问题讨论】:
-
如果你使用 api 为什么要显示 index.blade ? ,, 选择一个,你想检查 json 并用集合或刀片响应吗?