Search code examples
javascriptlaravelvue.jsvuejs2laravel-routing

Laravel + Vue 2 how to run certain functions on different pages


I am having this confussion, I have functions getUsers() and getCategories() (for instance) to retrieve all the users and categories in their respective pages /admin/users & /admin/categories.

I have a table where I load the users and in the other page another table to load the categories.

I was told to use them in created hook like:

created: function() {
    this.getUsers();
    this.getCategories();
}

But this causes those functions to load in the same page, I think this is unnecesary (maybe when I have 1 million records).

So I think what I need is to run this.getUsers() when I am on /admin/users and this.getCategories() when I am on /admin/categories.

Note: I am not using Vue-Router. Instead, I am using Laravel Routing

What should I do?


Solution

  • created: function() {
        var path = window.location.pathname;
        if(path === '/admin/users')
            this.getUsers();
        else if(path === '/admin/categories')
            this.getCategories();
    }