php - Laravel 路由到 Vue.js 路由

标签 php laravel vue.js

我的 web.php 路由文件中有大约 100 条路由,现在我开始使用 vue.js 和 laravel 而不是 Blade,所以我也应该在 vue 路由中写入所有 web.php 路由吗? 最好的方法是什么?

这是我的 web.php laravel 路线:

Route::post('/dashboard/widget', 'AdminDashboardController@widget')->name('dashboard.widget');

    Route::get('clients/export/{status?}/{client?}', ['uses' => 'ManageClientsController@export'])->name('clients.export');
    Route::get('clients/create/{clientID?}', ['uses' => 'ManageClientsController@create'])->name('clients.create');
    Route::post('clients', ['uses' => 'ManageClientsController@store'])->name('clients.store');
    Route::resource('clients', 'ManageClientsController', ['expect' => ['create']]);
    ..... and much more ......

我如何在 vue.js 中表示这个 laravel 路由,因为我的 web.php 中有超过 100 条路由

谢谢

最佳答案

有一个库可以做到这一点:https://github.com/tighten/ziggy

但只有当您的 Vue 组件加载到 Blade 模板内时它才有效,如果您有一个使用 Laravel API 的完全独立的 Vue 客户端,则它不起作用。假设第一种情况,只需安装库并将 @routes Blade 指令添加到主 Blade 文件中:

composer require tightenco/ziggy

resources/views/layouts/app.blade.php:

<head>
  <!-- ... -->

  <!-- CSRF Token -->
  <meta name="csrf-token" content="{{ csrf_token() }}">

  <title>{{ config('app.name', 'Laravel') }}</title>

  <!-- Routes -->
  @routes
  <!-- Scripts -->
  <script src="{{ asset('js/app.js') }}" defer></script>

  <!-- ... -->
<head>

然后您将在 Javascript 和 Vue 文件中使用 route 函数,其工作方式类似于 Laravel 提供的函数。

关于php - Laravel 路由到 Vue.js 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62596218/

相关文章:

php - Mysql 两小时之间获取数据

php - Laravel php 规范列表表

javascript - Vue.js 中的依赖循环问题

vue.js - Vue 生命周期 Hook 作为事件( Hook :beforeDestroy)

css - vue transition-group - 如何防止两个元素在 mode=out-in 中保留空间

php - 正则表达式检查带短掩码的 IP 地址

php - 中间表和 Laravel

php - PHP Websockets服务器在256个用户后停止接受连接

php - 如何获得具有 morphToMany 关系的第一个对象?

mysql - 如何获取 Laravel 查询的 $_GET 或 $_POST 值