简体   繁体   English

Laravel Vue 中多于 1 个惯性数据

[英]More than 1 inertia data in Laravel Vue

I am loading page with inertia in Laravel, but when I apply any filter it keeps GET/POST parameters from first render and also create new set of parameters.我在 Laravel 中加载惯性页面,但是当我应用任何过滤器时,它会保留来自第一次渲染的 GET/POST 参数,并创建新的参数集。

So if I apply filter again Inertia.post will run twice.因此,如果我再次应用过滤器,Inertia.post 将运行两次。 same will happen if I filter 3rd time.如果我第三次过滤,也会发生同样的情况。 Inertia.post will run 3 time. Inertia.post 将运行 3 次。 The code files are too large, so I am sharing the inertia part only.代码文件太大,所以我只分享惯性部分。 Hope that will be enough?希望这就足够了吗? Let me know if need more details please.如果需要更多详细信息,请告诉我。

Thank you guys.谢谢你们。

web.php网站.php

Route::get('sales', [SalesController::class, 'showSalesGraph'])->name('showSalesGraph');

SalesController.php销售控制器.php

return Inertia::render('Sales', [
            "sales" => $salesData,
            "startDate" => $startDate,
            "EndData" => $endDate
        ]);

sales.vue销售.vue

import { Inertia } from "@inertiajs/inertia";
import { computed, ref, reactive, inject, Vue } from "vue";

setup(props) {
   const postData = reactive({
        startDate: startDate,
        endDate:endDate,
        _token: usePage().props.value.csrf_token,
        change: "custom",
      });

   Inertia.get("sales", postData, {});
}

When you are using Inertia to fetch data, you should use当您使用 Inertia 获取数据时,您应该使用

Inertia.reload({
    only: ['properties'], 
    data: {}, 
    method: 'POST/PUT/GET/PATCH'
});

In your Controller, you can then conditionally load your InertiaData:在您的控制器中,您可以有条件地加载您的 InertiaData:

public function index(Request $request): \Inertia\Response 
{    
    $validatedRequest = $request->validate([
         'date_start' => 'required|datetime',
         'date_end'   => 'required|datetime',
    ]);
    
    return Inertia::render('Pages/Some/Component', [
         ...$validatedRequest,
         'data' => fn() => $this->loadGraphData($request),
    ]);
}

private function loadGraphData(Request $request): array
{
    // do something that might be heavy..
    return [];
}

What this allows you to do is to load the graph data only when it is requested OR on the first load.这允许您做的是仅在请求时或在第一次加载时加载图形数据。 You can read more about this in Inertia's documentation about Partial Reloads: https://inertiajs.com/partial-reloads您可以在 Inertia 关于部分重载的文档中阅读更多相关信息: https ://inertiajs.com/partial-reloads

If you want to submit standard forms, i would suggest using Inertia's useForm helper.如果您想提交标准表格,我建议您使用 Inertia 的useForm助手。

eg例如

<script setup>
const form = useForm({name: '', date_start: null, date_end: null});
</script>
<template>
    <input v-model="form.name"/>
</template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM