4

I would like to ask if can I implement this on vuejs, so basically the code will load a page/template base on the param url. I've been searching for a while and can't get the results I need or maybe I'm just searching a wrong keyword.

My url is like this, so I just can't manually declare the url in my route because it is dynamic, fetch from the database.

path: '/user/page_type

Thank you very much!

export default {
    mounted () {
        if(this.$routes.params.page_type == "home"){
            // Load Homepage Here
            // ../../../page/HomePage.vue
        }
        else if(this.$routes.params.page_type == "speaker"){
            // Load Speakerpage Here
            // ../../../page/HomePage.vue
        }
        else if(this.$routes.params.page_type == 'html'){
            // Load HTML Page Here
            // ../../../page/HtmlPage.vue
        }
    }
}

1 Answer 1

2

This is available out of the box within official addon vue-router.

Docs for your case: link

Sign up to request clarification or add additional context in comments.

5 Comments

My scenario is it will render different component based on params. Not the same component
Then in your /user/ component you can insert <component :is="getComponent"></component> and in script set watch: { '$route': { ... like described here router.vuejs.org/en/essentials/dynamic-matching.html (Reacting to Params Changes). In method getComponent you can return desired component name.
do you have any example for this <component :is="getComponent"></component> I'm quite didn't understand. would you mind to share some links that I can check?
You're the best Man!. This is what I'm looking for. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.