Skip to content

How do I build a URL from knowledge the Router has of all my routes? #569

Answered by lukepuplett
lukepuplett asked this question in Q&A
Discussion options

You must be logged in to vote

I am fortunately working with a React developer today!

Between us, we've found the .url(routeName, ...) method on the Router instance and that does exactly what I need!

Here's the help for it:

  /** Generate a URL pathname for a named route, interpolating the optional
   * params provided.  Also accepts an optional set of options. */

Here's it in use in context:

export const routes = new Router()
  .get(
    "get-test",
    "/test",
    handleGetTest,
  );

function handleGetTest(context: Context) {
  console.log(`The URL for the test route is: ${routes.url("get-test")}`);
}

// The URL for the test route is: /test

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by lukepuplett
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant