Skip to content
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

Store Router not updating #11

Open
arthurgermano opened this issue May 14, 2020 · 2 comments
Open

Store Router not updating #11

arthurgermano opened this issue May 14, 2020 · 2 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@arthurgermano
Copy link

Hi there.. the router store is not updating or reacting..

My guess that is not creating an object or array brand new.. causing Svelte not realizing that the object/store has changed.
Screenshot from 2020-05-14 13-22-37

Anyway.. many thanks!
=)

@rraallvv
Copy link

rraallvv commented May 28, 2020

Any idea how to solve this issue?

When I added the router import to the template in the file svelte-router-template/src/js/app.svelte where createRouter is called but $router appears always as undefined:

<script>
//...
  import createRouter, {ROUTER_MODE, router} from '@spaceavocado/svelte-router';
//...
  setInterval(() => {
    console.log($router)
  }, 2000);
</script>

@davidhorak davidhorak added question Further information is requested help wanted Extra attention is needed labels Dec 11, 2020
@CherryDT
Copy link

CherryDT commented Dec 14, 2020

$router provides only the router itself, it won't update when the router object mutates, as far as I understand. You can use the onNavigationChanged method to actually listen for navigations. I did it like this:

let unregister = $router.onNavigationChanged((from, to) => { /* do stuff */ })

onDestroy(() => {
  unregister()
  unregister = null
})

I also needed to access the router in the same file in which I created it, though (in order to add a navigation guard and error handler), so I used the following helper function:

function getStoreValue (store) {
  let value
  store.subscribe(v => (value = v))()
  return value
}

(Note the extra () which unsubscribes again immediately.)

I then did this, using the return value of createRouter in getStoreValue(router) instead of $router:

const router = createRouter({ /* routes here */ })

getStoreValue(router).navigationGuard((from, to, next) => { /* navigation guard here */ })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants