Skip to content

How to use Global Before Guards #404

Answered by jakobrosenberg
yangshangbo asked this question in Q&A
Discussion options

You must be logged in to vote

In your root _module.svelte, insert a module like this

<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 guard signature may change in future iterations.

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@yangshangbo
Comment options

Comment options

You must be logged in to vote
1 reply
@yangshangbo
Comment options

Answer selected by yangshangbo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants