我的 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/