Search code examples
laravelvuejs3inertiajs

How to get value of current page in setup method of inertiajs 3 app?


In which way can I in laravel 10 / inertiajs 3 app get value of current page, whe using pagination and url like :

http://127.0.0.1:8000/?page=3'

?

I tried window.location - has data:

href: 'http://127.0.0.1:8000/?page=3', origin: 'http://127.0.0.1:8000', protocol: 'http:', host: '127.0.0.1:8000', …}

and window.location.href :

http://127.0.0.1:8000/?page=3

I mean without parsing string value...


Solution

  • You can achieve this in two ways:

    1. Using URLSearchParams javascript API:
    const page = new URLSearchParams(window.location.search).get('page');
    console.log(page); // "3"
    
    1. Using built-in Inetria's router:
    import { router } from '@inertiajs/vue3';
    
    const page = router.page.props.query?.page;
    console.log(page); // "3"