简体   繁体   English

使用 Laravel Inertia 观察路线变化

[英]Watching route change with Laravel Inertia

Using the new Inertia Laravel stack – how do you watch route change?使用新的 Inertia Laravel 堆栈 – 您如何观察路线变化?

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.您需要收听start事件。

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.请注意,在加载完成之前触发start事件。 If you want to get notified once the new page is displayed, use the success event.如果您想在显示新页面后得到通知,请使用success事件。

For more info, check out https://inertiajs.com/events有关更多信息,请查看https://inertiajs.com/events

You can also do:你也可以这样做:

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

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

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