question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use goto in a Svelte module? "goto is not a function"

See original GitHub issue

Hi,

I’m coding a module with some functions to be used elsewhere and I’m having trouble to use goto, here some example:

functions.svelte

<script context="module">
    import { goto } from "@roxi/routify";

export function doSomething() {
    console.log("did it");
    goto("/newpage");
}
</script>

After that, I import the function in my App:

<script>
   import { doSomething } from "./functions.svelte";

  doSomething();
</script>

When running, I get the error “goto is not a function”

What is the proper way to do this?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ferulissescommented, Aug 3, 2021

Hum, I tried using window.location.href, but it loses data on stores (unless saved on browser, what I’m not inclined to do), guess I will need to move goto to the page.

Thanks anyway.

0reactions
jakobrosenbergcommented, Aug 4, 2021

@ferulisses if you don’t need dynamic redirects, you can also use this:

  • redirect redirects before component is loaded
  • rewrite same as redirect, but preserves the URL
<!-- Redirect example. Same for rewrite.-->

<!-- routify:options redirect="/some/path" -->
<!-- routify:options redirect={"path": "/some/path", "params": {"id": "1"} -->

You can also create guards with beforeUrlChange.

For context="module" guards you will have to wait for Routify 3, which we hope to release a preview of very soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

`svelte-kit package` cannot include `goto` function from `$app ...
Everything works just fine in dev. I import the Select component I created into my project. However, when the app is build and...
Read more >
Cannot use goto on the server : r/sveltejs - Reddit
I want to redirect the user to the login page if they are not logged in, but for some reason it gives out...
Read more >
Going back to the previous page with 'goto' / Sveltekit navigation
in our SvelteKit project we are sometimes navigating to previous page like in the example bellow import { goto } from '$app/navigation'; ...
Read more >
Modules • Docs • SvelteKit
Returns a Promise that resolves when SvelteKit navigates (or fails to navigate, in which case the promise rejects) to the specified url ....
Read more >
Authentication in Svelte using cookies - LogRocket Blog
Create a Svelte app using SvelteKit that implements a cookie for ... SvelteKit provides its own function for client-side navigation: goto .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found