如何结合Laravel和Vue路线 [英] How to combine laravel and vue routes

查看:71
本文介绍了如何结合Laravel和Vue路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a simple laravel and vuejs CRUD Application. Vue Routes are not working, I am pretty new to vuejs; please see the code Below is the code for 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的代码

Following is the code for 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.部署生产时,请确保打开生产模式.

This is the code of my blade tamplate, when I browse http://localhost:8000/vue this view is being rendered. As you can see in the web.php code above. I can also see the notification in console You are running Vue in development mode. Make sure to turn on production mode when deploying for production.

   <!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错误.请帮助我解决这个问题.

Application show a 404 error. Please help me with this problem.

推荐答案

您需要在 routes/web.php 文件.

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

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

and then you have to use the laravel route as a parent route for the vuejs's routes and use the url like below,

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

这篇关于如何结合Laravel和Vue路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆