Laravel and Vue JS Route



如何在 Vue 组件中使用我的 laravel web.php's route('test.create'(? 这是网络.php:

Route::resource('/test', 'TestController');

但是我想通过路由('test.create'(访问网址并将其添加到我的 Vue 组件的<form>中。 这是 Vue 组件:

<template>
<div id="app">
<form action="{{route('test.create')}}" method="POST">
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" class="form-control"> 
</div>
<div class="form-group">
<label for="location">Location</label>
<input type="text" id="location" class="form-control"> 
</div>
<div class="form-group">
<label for="age">Age</label>
<input type="number" id="age" class="form-control"> 
</div>
<div class="form-group">
<input type="submit" class="btn btn-default"> 
</div>
</form>
</div>
</template>
<script>
export default {
name: 'create',
data: function(){
return {
msg: '',
}
},
}
</script>

您无法访问route()它是一个Laravel helper function它只能在内部工作

/

资源/视图/

route('test.create')

是一个Laravel函数,它不能在Vue组件/JavaScript上工作。你需要通过 DOM 将返回的值传递给 vue 组件(在你的模板/刀片 php 文件中(。

例如:

在你的观点中.php刀片:

<your-component create-url="{{route('test.create')}}"></your-component>

在你的组件.vue:

props: ['createUrl']

然后你可以使用

<form action="{{createUrl}}" method="POST">

最新更新