-
-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nested routes click to parent problem #56
Comments
Hi @rottmann sorry for the delay in responding. This is actually not an issue in the router itself, but it's how Svelte works. The "problem" is that your code never causes the <script>
import Router, { push } from 'svelte-spa-router'
import CallEditView from './CallEditView.svelte'
import { onMount } from 'svelte'
const routes = {
'/call/edit/:id': CallEditView
}
onMount(async () => {
console.log('mounted')
})
// this is only called one time, when the page load, but not wenn you push the button
// (onMount / async stuff fake)
setTimeout( () => {
push('/call/edit/1')
}, 1000)
</script>
<button on:click={() => push('/call/') }>Go to /call/</button><br>
<Router {routes}></Router> In the console, you'll see that "mounted" appears only once. This is by design. There's a simple workaround, however, which is to listen on the <script>
import Router, {push, location} from 'svelte-spa-router'
import CallEditView from './CallEditView.svelte'
import {onDestroy} from 'svelte'
const routes = {
'/call/edit/:id': CallEditView
}
const unsubscribe = location.subscribe((val) => {
setTimeout( () => {
console.log('pushing')
push('/call/edit/1')
}, 1000)
})
onDestroy(async () => {
unsubscribe()
})
</script>
<button on:click={() => push('/call/') }>Go to /call/</button><br>
<Router {routes}></Router> |
Thank you! Other solution could be to react on location change with |
The two things are equivalent. The $ syntax behind the scene manages the subscription/un-subscription automatically, as per the svelte documentation! |
Example:
App.svelte
CallView.svelte
CallEditView.svelte
Open http://localhost:5000/#/call/
after 1 second CallView pushes to nested view /call/edit/1
click the button,
no nested view is visible, cause setTimeout/onMount is only called once.
The text was updated successfully, but these errors were encountered: