2016-09-26 189 views
3

我已經成功安裝了vue-router,但是我有一些問題與我的laravel 5.3路由混合。vue-router與laravel路由相結合

我有回家的PHP路線:

Route::get('/', array('as' => 'home', 'uses' => '[email protected]')); 

和我有安裝VUE路由器路線:

'/user-feed': { 
    name: 'user-feed', 
    title: 'User Feed', 
    component: Feed 
}, 

'*': { 
    component: PageNotFound 
} 

在我web.php路由文件,我有以下幾點:

Route::get('/', array('as' => 'home', 'uses' => '[email protected]')); 

// use vue-router route 
Route::get('/{subs}', [function() { 
    return view('layouts'); 
}])->where(['subs' => '.*']); 

我的問題是當我改變路由的順序(vue-router在家庭路由器之前)並且我嘗試訪問home路由,Page not found呈現vue-route並且user-feed可通過v-link使用。

當VUE路由器是home路線後,家是正確的渲染,但vue-router不可訪問通過v-link,我可以訪問VUE路由器的唯一方法是通過手動輸入的URL。

如何將vue-router與我的laravel route一起使用?

回答

3

你真的不應該一起使用這兩個前端導航,除非你有一個合乎邏輯的理由。 Vue路由器專用於單頁應用程序,Laravel的路由系統專用於多頁面應用程序或API。我認爲最常見的設置是讓所有的Laravel路線除了說/api/重定向到包含您的SPA的單個模板,例如app.blade.php。從那裏你的vue路由器將是你的前端導航,你的端點將是你如何獲取數據進出laravel。

+1

我需要的,因爲我仍然在轉換我的刀片頁面,Vue公司的過程中同時使用兩者。所以在這種情況下,不是所有的頁面都會被vue驅動 – raffffffff

+0

你真的不應該那樣做,但是如果你需要的話,我會建議你不要使用v-link,而你仍然需要鏈接到laravel生成的頁面。 – jostrander

0

試試這個,這可能解決您的問題

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' 
});