vue-router in conjunction with laravel routes

前端 未结 2 1785
北海茫月
北海茫月 2021-01-14 15:39

I have setup vue-router successfully but I have some problem mixing it with my laravel 5.3 routes.

I have a php route for home:

Route::get(\'/\', arr         


        
相关标签:
2条回答
  • 2021-01-14 16:28

    You really shouldn't be using the two together for front end navigation, unless you have a logical reason to. Vue router is intended for single page applications, and Laravel's routing system is intended for multi-page applications, or APIs. The most common set up I think would be to have all Laravel routes except for say /api/ redirect to a single template that includes your SPA, say app.blade.php. From there your vue-router will be your front end navigation and your /api/ endpoints will be how you get data in and out of laravel.

    0 讨论(0)
  • 2021-01-14 16:29

    try this this may solve your problem

    export var router = new Router({
      hashbang: false,
      history: true,
      linkActiveClass: 'active',
      mode: 'html5'
    });
    router.map({
      '/': {
        name: 'home',
        component: Home
      },
      '/dashboard': {
        name: 'dashboard',
        component: Dashboard
      },
      '/404notfound': {
        name: 'pagenotfound',
        component: Pagenotfound
      }
    });
    
    // For every new route scroll to the top of the page
    router.beforeEach(function() {  
      window.scrollTo(0, 0);
    });
    
    // If no route is matched redirect home
    router.redirect({
      '*': '/404notfound'
    });
    
    0 讨论(0)
提交回复
热议问题