laravel Vue 中的自定义表单操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48703724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom form action in Vue
提问by Neha
i am using form in Vuejs component template. i don't want to define full static URL. the page URL is like http://example.com/en/discussesand form should submit at http://example.com/comments. but using
我在 Vuejs 组件模板中使用表单。我不想定义完整的静态 URL。页面 URL 类似于http://example.com/en/discusses并且表单应在http://example.com/comments提交。但使用
<form action = "comments" method ="POST">
goes to http://example.com/en/commentsinstead of http://example.com/comments. How to achieve this?. Thank you in advance.
转到http://example.com/en/comments而不是http://example.com/comments。如何实现这一目标?先感谢您。
回答by jhnbrn
You can give your post route a name in the routes file (routes/web.php):
您可以在路由文件 (routes/web.php) 中为您的 post 路由命名:
For example:
例如:
Route::post('/comments', 'SomeController@store')->name('postComment');
Normally then, you can use the named route in your form in the blade view:
通常,您可以在刀片视图中的表单中使用命名路由:
<form action="{{ route('postComment') }}" method="POST">
</form>
In this case you use a Vue component, you can pass the 'postComment' route
在这种情况下,您使用 Vue 组件,您可以传递 'postComment' 路由
<component post-route="{{ route('postComment') }}"></component>
Then, accept the property in your Vue component file:
然后,接受 Vue 组件文件中的属性:
<template>
<form action="this.post-route" method="POST"></form>
</template>
<script>
export default {
props: ['post-route']
}
</script>
回答by Piotr ?ak
for ajax calls I'm using axios it has axios.default.baseUrl = 'yourBaseUrl'
对于 ajax 调用,我使用的是 axios,它有 axios.default.baseUrl = 'yourBaseUrl'
yourBaseUrl is added before every ajax call You do.
yourBaseUrl 是在每次 ajax 调用之前添加的。