Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 787
StevenThelin Avatar asked Mar 24 '26 00:03

StevenThelin


1 Answers

  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().

like image 99
Linus Borg Avatar answered Mar 25 '26 15:03

Linus Borg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!