-
Notifications
You must be signed in to change notification settings - Fork 177
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
Access path in other components #41
Comments
Hi @iamfrntdv! There is currently no nice way to do this. We could possibly take some more inspiration from Reach Router and give the We could possibly expose |
How about creating global state or variable so it will be available everywhere, something like vue-router does? |
Interesting. I have very limited experience with vue-router, and I'm not exactly sure what behaviour you are referring to. Could you elaborate? |
@iamfrntdv How about the following approach?
|
@kysonic hmmm, this may work. I'll try it and let you know. Thanks. |
Thanks @kysonic, your solution works fine for me. I moved my <Router>
<NavBar isLoggedIn={isLoggedIn} loggedInUser={loggedInUser} />
<Route exact path="/">
<Home isLoggedIn={isLoggedIn} loggedInUser={loggedInUser} />
</Route>
<Route path="/panels" component={Panels} />
<Route path="/users" component={Users} />
<Route path="/login" component={Login} />
<Route path="*" component={NotFound} />
</Router> Then on the <script>
import { getContext } from 'svelte'
import { ROUTER } from 'svelte-routing/src/contexts'
var { activeRoute } = getContext(ROUTER)
...
</script>
<main>
...
{#if !isLoggedIn && ($activeRoute ? $activeRoute.uri : '') != '/login'}
<button type="button" on:click={()=>navigate('/login')}>Login</button>
{:else if isLoggedIn}
<span >{loggedInUser.displayName ? loggedInUser.displayName : ''}</span>
<button type="button" on:click={logout} outline>Logout</button>
{/if}
...
</main> This way the the login button doesn't get displayed on the |
I have another workaround for when you are in the topmost component, so <script>
import {Router, Route} from 'svelte-routing';
import {globalHistory} from 'svelte-routing/src/history';
import {route} from './stores.js';
$route = globalHistory.location;
globalHistory.listen(history => {
$route = history.location;
});
</script>
<Router>
<!-- other routes... -->
<Route path="/" component={Home}/>
</Router>
{#if $route.pathname === '/'}
<img src="logo-home.svg">
{:else}
<img src="logo-general.svg">
{/if} This way you have the variable |
@kysonic @henk23 Do these methods still work for you? When I try to use ROUTER context its undefined (I am inside the router context when trying it, in fact have tried moving it all over.) When I try the globalHistory.listen method I seem to end up with 2 instances of globalHistory and my listener doesn't get called. I don't know how that is even possible. I have a router embedded in another router, though looking at the code I think this should be fine. If anyone has insight, it'd be appreciated. |
I found a solution in case anyone runs into the same issue. It's a bundler problem. The router was using globalHistory from one place but when I directly imported My bundler was grabbing it from a different place. The solution was to add globalHistory to the exports for svelte-routing and import it from there . |
Svelte Routing v1.8 has been released, which added |
@krishnaTORQUE How can i use the useLocation hook? It does not react to changes and gives me undefined everytime |
Is there any way to access/watch current path in components that are available in all routes for example header. I would like to show different links based on current path.
Is there any preferred way to day this?
The text was updated successfully, but these errors were encountered: