From 4d7f55bce7e42fec26943cf3a7cd053a413c94dc Mon Sep 17 00:00:00 2001 From: Antoine Lelaisant Date: Sat, 19 Oct 2019 01:06:17 +0200 Subject: [PATCH] Named routes --- README.md | 66 +++++++++++++++++++++++++++++++++--- src/components/Link.svelte | 28 +++++++++++++++ src/components/Route.svelte | 21 +++++++++++- src/components/Router.svelte | 6 +++- src/index.js | 1 + src/store.js | 1 + src/utils.js | 6 ++++ 7 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 src/components/Link.svelte diff --git a/README.md b/README.md index 7a65130..2d9d5c6 100644 --- a/README.md +++ b/README.md @@ -49,12 +49,12 @@ yarn add svelters-router let routes = [ { - path: "/users", - component: Users + path: "/users", + component: Users }, { - path: "/users/:id", - component: User + path: "/users/:id", + component: User } ] @@ -101,6 +101,8 @@ Route parameters are compiled by `svelters-router` and given to defined componen ### Links +#### The link action + ```svelte + +

I wonder how simple it is to create a link ?

+ +Just click here! +``` + ### To navigate somewhere programmatically ```svelte @@ -129,3 +143,47 @@ Route parameters are compiled by `svelters-router` and given to defined componen ``` + +## Advanced + +### Named routes + +It's sometime more convenient to rely on route name instead of its path to generate links. + +`svelters-router` provides all the stuff you need! + +#### Route declaration + +```svelte + + + + + + +``` + +#### Link to a named route + +```svelte + + +See the user {userId} +``` diff --git a/src/components/Link.svelte b/src/components/Link.svelte new file mode 100644 index 0000000..3329a58 --- /dev/null +++ b/src/components/Link.svelte @@ -0,0 +1,28 @@ + + + + + diff --git a/src/components/Route.svelte b/src/components/Route.svelte index 008d355..81ac740 100644 --- a/src/components/Route.svelte +++ b/src/components/Route.svelte @@ -1,12 +1,31 @@ {#each routes as route} - + {/each} diff --git a/src/index.js b/src/index.js index 94c2e52..a5ef086 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ export { default as Router } from './components/Router.svelte' export { default as Route } from './components/Route.svelte' +export { default as Link } from './components/Link.svelte' export { push, replace, goBack, goForward } from './history' export { default as link } from './link' diff --git a/src/store.js b/src/store.js index 928c188..ef71a06 100644 --- a/src/store.js +++ b/src/store.js @@ -1,3 +1,4 @@ import { writable } from 'svelte/store' export const location = writable(null) +export const routes = writable([]) diff --git a/src/utils.js b/src/utils.js index b7dc45a..cd915b2 100644 --- a/src/utils.js +++ b/src/utils.js @@ -19,3 +19,9 @@ export const parseParameters = (location, path) => { return memo }, {}) } + +export const compileParameters = (path, parameters) => { + const toPath = pathToRegexp.compile(path) + + return toPath(parameters) +}