這是我的產品控制器
public function index()
{
return Product::all();
}
我正在尋找一種方法將此方法從慣性請求回傳到我的 Vue 組件,這是我嘗試過的方法,
路線/web.php
Route::get('/', function () {
return Inertia::render('App', [ProductController::class, 'index']);
});
這是我的 Vue 組件(與我嘗試獲取資料的方式不同)
<template>
</template>
<script>
props: {
index: Array,
},
</script>
uj5u.com熱心網友回復:
在您的控制器中,您可以將產品串列傳遞給您的組件:
public function index()
{
return Inertia::render('App', [
'products' => Product::all()
]);
}
檔案:https ://inertiajs.com/responses
然后像這樣宣告你的路線:
Route::get('/products', [ProductController::class, 'index']);
這將創建一個/products指向控制器的 index 操作的路由,該操作會將產品串列回傳到您的組件。
在您的組件中,您可以訪問產品:
<template>
<div>
{{ products.length }}
</div>
</template>
<script>
props: {
products: Array,
},
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/490515.html
