我有一个Vue.js组件,它通过Axios请求Laravel路由。但是在Vue文件中,我无法访问route()
函数,现在只能使用静态URL。以下是一些代码:
web.php:
// API
Route::group(['prefix' => 'api'], function() {
// GET
Route::get('/brands', 'BrandsController@getBrands')->name('api-get-brands');
Route::get('/{brand_id}/models', 'BrandsController@getModels')->name('api-get-models');
Route::get('/fuel-types', 'BrandsController@getFuelTypes')->name('api-get-fuel-types');
Route::get('/gearboxes', 'BrandsController@getGearboxes')->name('api-get-gearboxes');
});
Vue 组件:
methods: {
getBrands: function() {
let app = this
axios.get('/api/brands')
.then(function(response) {
app.brands = response.data
})
.catch(function(error) {
console.log(error)
})
},
...
现在它可以工作了,但我想知道这是否是最好的方法,或者有更好的方法可以做到这一点。
window
变量,取得了相当不错的成功。 - Ohgodwhyphp artisan laroute:generate
? - ljubadrroutes
目录的更改。在监听器的回调中,你可以运行Artisan::call('laroute:generate')
来重新生成路由文件。然后你可以运行npm run watch
或npm run hot
,它将自动重新编译你的JS,包括新的路由,只要你在你的javascript中的某个地方做了类似于require('routes.js)
的事情。 - Ohgodwhy