简体   繁体   English

Laravel Vue Inertia 动态分页安装错误

[英]Laravel Vue Inertia dynamic pagination bugging with mount

I'm using a dynamic query in Laravel Inertia, my pagination is bugging because every time i try to change the pagination the mount() method of vue runs.我在 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),
        },
    },

I need the mount() only on the first time or the component get rendered, but I dont want the mount() function to run everytime I use or change my pagination, is there any work around for this?我只在第一次需要 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(),

This is my query.这是我的查询。

i'm not sure why state isn't being preserved in your case, i would need to test it.我不确定为什么在您的情况下没有保留状态,我需要对其进行测试。

I handle it slightly differently (example using the composition api)我的处理方式略有不同(使用组合 API 的示例)

I don't do a request on mounted, that seems kind of redundant and maybe what's causing your issue, you shouldn't need to filter on mounted as when you are initially mounted it should be your initial request and the user didn't change anything in the fiters yet我没有在挂载时提出请求,这似乎有点多余,也许是导致您的问题的原因,您不需要在挂载时进行过滤,因为当您最初挂载时,它应该是您的初始请求并且用户没有更改任何东西都还可以


// 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