简体   繁体   中英

Watching route change with Laravel Inertia

Using the new Inertia Laravel stack – how do you watch route change?

I am trying to do something very simple in the likes of:

watch: {
  route () {
    console.log('You changed route')
  }
}

I haven't been able to find any information on this.

You'll need to listen to the start event.

import { Inertia } from '@inertiajs/inertia'

Inertia.on('start', (event) => {
  console.log(`The route changed to ${event.detail.visit.url}`)
})

Note the start event is triggered before the load completes. If you want to get notified once the new page is displayed, use the success event.

For more info, check out https://inertiajs.com/events

You can also do:

watch: {
  '$page.url': function (newUrl, oldUrl) {
    console.log(newUrl)
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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