How to use Global Before Guards #404
Answered
by
jakobrosenberg
yangshangbo
asked this question in
Q&A
-
i use it in index.md file <script>
import { context } from "@roxi/routify";
$context.route.router.beforeUrlChange((route) => {
if (/[A-Z]/g.test(route.url)) {
console.error("搜索内容中不能包含大写字母");
return Promise.reject();
} else {
return Promise.resolve();
}
});
</script>
<slot></slot> but it does not work; |
Beta Was this translation helpful? Give feedback.
Answered by
jakobrosenberg
Nov 4, 2021
Replies: 2 comments 2 replies
-
Is this about Routify 2 or 3? |
Beta Was this translation helpful? Give feedback.
1 reply
-
In your root <script context="module">
// the guard module stops navigation if the return value isn't truthy or a truthy promise
export const guard = route => {
if (/[A-Z]/g.test(route.url)) {
console.error("搜索内容中不能包含大写字母");
// if you need to redirect (note: `replace` works like replaceState, but you can also use `push` )
route.router.url.replace('/')
} else
return true
}
</script> Please note that the callback |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
yangshangbo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In your root
_module.svelte
, insert a module like thisPlease note that the callback
guard
signature may change in future iterations.