StevenThelin
StevenThelin

Reputation: 235

Using Vue dynamic route name, in component function

I've been trying to make a dynamic view rendering, with Vue and Laravel. However, i can't wrap my head around how i am supposed to parse the dynamic parameter, to the component function.

Router.map({
    '/cms-admin/:page': {
        component: {
            template: returnView(this.$route.params.page)
        }
    }
});

function returnView (option) {
    // Generate the AJAX request here
}

Through documentations i've read, that $route should solve the issue. I can parse $route into the view, and print the text on the page. However, i can't use $route inside the map, to get the dynamic name?

Say, i enter "/cms-admin/dashboard", 'dashboard' should get parsed down to the template parameters.

Thanks in advance, Steven

Upvotes: 3

Views: 2600

Answers (1)

Linus Borg
Linus Borg

Reputation: 23998

  1. register the individual templates for the pages as partials v1 v2
  2. use <partial> and $route

js:

component: {
  template: '<partial :name="partial" v-if="partial !== ''"></partial>',
  data() { return { partial: '' } },
  ready() { this.partial = this.$route.params.page},
}

Note: Not sure whether you can access this.$route in data(), therefore I used the ready() event, but maybe you can drop that and put it directly in data().

Upvotes: 5

Related Questions