我有集成了 Vue.js 的 Laravel 5.3 项目,我想在我的表单中使用 CSRF-TOKEN
。表单html代码在Vue组件文件中
resources / assets / js / bootstrap.js
我有这个:
Vue.http.interceptors.push((request, next) => {
request.headers.set('X-CSRF-TOKEN', MyApp.csrfToken);
next();
});
然后我有主vue文件 /resources/assets/js/app.js
:
require('./bootstrap');
Vue.component('callbackwindow', require('./components/callbackwindow.vue'));
const app = new Vue({
el: '#app',
data: { },
});
然后在 Vue 文件中我需要使用 csrf_field
,但我不知道如何得到它,因为标准 php csrf_field()
没有在 Vue 组件中呈现,我不知道如何导入 MyApp.csrfToken
。
<template>
<div class="modal fade" >
<form @submit.prevent="submitForm" name="callback" method="POST" action="/" role="form" class="form-horizontal" enctype="multipart/form-data">
{!! csrf_field() !!}
...form code here...
</form>
</div>
</template>
<script>
export default { }
</script>
是否可以将 MyApp.csrfToken
变量从这里导入到我的 Vue 模板文件中?
原文由 schel4ok 发布,翻译遵循 CC BY-SA 4.0 许可协议
作为替代方法:
1- 从带有
csrf-token
名称在<head>
的元标记中获取令牌:2-将其作为道具传递给您的 Vue 组件: