How can i watch for route change in app.js

This issue has been tracked since 2022-08-24.

Hey there,
thank you for this awesome Router. In a current Project i need to be able to watch for route changes globally, so i need to have a function in app.js wich checks if a Route has been changed. Currently i see no possible Solution, are there any?
Thanks!

amorfati254 wrote this answer on 2022-10-17

Hey there, did you find solution to this problem?

julianlaibach wrote this answer on 2022-10-17

Hey @amorfati254 sure! I did the following:

in your main JS File in my case: app.js i have the following Lines:

import {route} from "./stores/route"

addEventListener("popstate", function (e) {
    onRouteChanged()
});

const onRouteChanged = () => {
    route.set(window.location)    
    scrollTo(0, 0)
}

Then i created a route store with the following Lines:

import { writable } from 'svelte/store'

export const route = writable()

Now you can do:

route.subscribe(route => {
    console.log(route)
});

Hope this is helpful!

More Details About Repo
Owner Name EmilTholin
Repo Name svelte-routing
Full Name EmilTholin/svelte-routing
Language JavaScript
Created Date 2017-11-28
Updated Date 2022-12-03
Star Count 1711
Watcher Count 21
Fork Count 151
Issue Count 59

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date