繁体   English   中英

Laravel Vue Inertia 动态分页安装错误

[英]Laravel Vue Inertia dynamic pagination bugging with mount

我在 Laravel Inertia 中使用动态查询,我的分页出错了,因为每次我尝试更改分页时 vue 的 mount() 方法都会运行。

 data() {
        return {
            form: this.$inertia.form({
                search: this.filters.search,
                order: "asc",
                column: "code",
                size: 5,
            }),
        };
    },
    mounted() {
        this.$inertia.get(this.route("groups.index"), pickBy(this.form), {
            preserveState: true,
        });
    },
    watch: {
        form: {
            deep: true,
            handler: throttle(function () {
                this.$inertia.get(
                    this.route("groups.index"),
                    pickBy(this.form),
                    { preserveState: true }
                );
            }, 150),
        },
    },

我只在第一次需要 mount() 或组件被渲染,但我不希望 mount() 函数在我每次使用或更改分页时都运行,有什么解决方法吗?

'groups' => Group::where('company_id', Auth::user()->company_id)
            ->when($request->search, function($query, $term){
                $query->where('code', 'LIKE' , '%' . $term. '%')
                      ->orWhere('name', 'LIKE' , '%' . $term. '%')
                      ->orWhere('description', 'LIKE' , '%' . $term. '%');
            })->when($request->column, function($query, $term) use ($request){
                $query->orderBy($term, $request->order);
            })->paginate($request->size)->withQueryString(),

这是我的查询。

我不确定为什么在您的情况下没有保留状态,我需要对其进行测试。

我的处理方式略有不同(使用组合 API 的示例)

我没有在挂载时提出请求,这似乎有点多余,也许是导致您的问题的原因,您不需要在挂载时进行过滤,因为当您最初挂载时,它应该是您的初始请求并且用户没有更改任何东西都还可以


// you can also get the current filters from route().params
// current filters coming from the server
const props = defineProps({
    filters: Object,
})

const form = reactive({
    search: props.filters.search,
    order: props.filters.order,
    column: props.filters.column,
    size: props.filters.size,
})

const filter = throttle(function() {
    Inertia.get(route(route().current()), pickBy(form), {
        preserveState: true,
        preserveScroll: true,
    })
}, 150)

watch(form, filter)

暂无
暂无

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

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