Sydney Loteria
Sydney Loteria

Reputation: 10441

Load component/template depending on the route param vuejs

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
        }
    }
}

Upvotes: 4

Views: 6425

Answers (1)

Egor Stambakio
Egor Stambakio

Reputation: 18126

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

Docs for your case: link

Upvotes: 2

Related Questions