首页 > 解决方案 > 如何结合 laravel 和 vue 路由

问题描述

我正在创建一个简单的 laravel 和 vuejs CRUD 应用程序。Vue Routes 不起作用,我对 vuejs 很陌生;请看代码下面是web.php的代码

    Route::get('/', function () {
    return view('welcome');
    });

    Auth::routes();

    Route::get('/home', 'HomeController@index')->name('home');
    Route::get('/vue','Api\PostController@home');
    Route::resource('/api','Api\PostController');

以下是 app.js 的代码

    require('./bootstrap');

    window.Vue = require('vue');

    window.VueRouter=require('vue-router').default;

    window.VueAxios=require('vue-axios').default;

    window.Axios=require('axios').default;

    let AppLayout = require('./components/App.vue');
    const Posts = Vue.component('Posts',require('./components/Posts.vue'));
    const EditPost = 
    Vue.component('EditPost',require('./components/EditPost.vue'));
    const AddPost = 
    Vue.component('AddPost',require('./components/AddPost.vue'));
    const DeletePost = 
    Vue.component('DeletePost',require('./components/AddPost.vue'));
    const ViewPosts = 
    Vue.component('ViewPosts',require('./components/ViewPosts.vue'));
    const ExampleComponent = 
   Vue.component('ViewPosts',require('./components/ExampleComponent.vue'));

   // Registering routes
   Vue.use(VueRouter,VueAxios,axios);

   const routes = [
   {
    name: 'Posts',
    path: '/posts',
    component: Posts
   },
   {
    name: 'AddPost',
    path: '/add-posts',
    component: AddPost
   },
   {
    name: 'EditPost',
    path: '/edit-post/:id',
    component: EditPost
   },
   {
    name: 'DeletePost',
    path: '/delete-post',
    component: DeletePost
   },
   {
    name: 'ViewPosts',
    path: '/view-post',
    component: ViewPosts
   },
   {
    name: 'ExampleComponent',
    path: '/example-component',
    component: ExampleComponent
   },
 ];
    const router = new VueRouter({mode: 'history', routes: routes});
    new Vue(
      Vue.util.extend(
      { router },
      AppLayout
     )).$mount('#app');

这是我的刀片模板的代码,当我浏览http://localhost:8000/vue时,正在呈现此视图。正如您在上面的 web.php 代码中看到的那样。我还可以在控制台中看到通知您正在开发模式下运行 Vue。确保在部署生产时打开生产模式。

   <!DOCTYPE html>
   <html lang="{{ app()->getLocale() }}">
   <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>{{ config('app.name', 'Laravel') }}</title>
<!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
  </head>
  <body>
   <div class="container">
   <header class="page-header">
    <div class="branding">
        <img src="https://vuejs.org/images/logo.png" alt="Logo" title="Home page" class="logo"/>
        <h1>Vue.js CRUD With Laravel 5 application</h1>
    </div>
   </header>
 </div>
<section id="app">

</section>
<script>
  window.Laravel = <?php echo json_encode([
    'csrfToken' => csrf_token(),
]); ?>
</script>
<script src="{{ asset('js/app.js') }}"></script>
 </body>
 </html>

但是当我使用

    php artisan serve

并浏览到

    http://localhost:8000/posts

应用程序显示 404 错误。请帮我解决这个问题。

标签: phplaravelvue.js

解决方案


您需要为在routes/web.php文件中使用 app.js (vuejs) 的视图添加 laravel 路由。

Route::get('/route-name/?{name}', function(){
    return redirect('vue_app');
})->where('name', '[A-Za-z]+');

然后你必须使用 laravel 路由作为 vuejs 路由的父路由,并使用如下所示的 url,

http://localhost:8000/laravel-route/view-route

在你的情况下,

http://localhost:8000/route-name/posts

或者你也可以使用,

Route::get('{any}', function () { 
    return view('vue_app'); 
})->where('any', '.*'); 

而不是以前的使用localhost:8000/posts


推荐阅读