我想问一下如何在我的 vue 组件中添加 csrf_field() 。错误是
属性或方法“csrfToken”未在实例上定义,但在渲染期间引用。确保在数据选项中声明反应数据属性。
这是代码:
<script>
export default {
name: 'create',
data: function(){
return {
msg: ''
}
},
props:['test']
}
</script>
<template>
<div id="app">
<form action="#" method="POST">
{{csrfToken()}}
<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>
正如我已经写过的here https://stackoverflow.com/questions/39938284/how-to-pass-laravel-csrf-token-value-to-vue/47715332,我只是建议将其放入您的 PHP 文件中:
<script>
window.Laravel = <?php echo json_encode(['csrfToken' => csrf_token()]); ?>
</script>
这样您就可以轻松地从 JS 部分(本例中为 Vue)导入 csrfToken。
此外,如果您将此代码插入 PHP 布局文件中,则可以通过应用程序的任何组件使用该令牌,因为window
是一个JS全局变量。
来源:我的技巧来自this https://appdividend.com/2017/08/05/laravel-vuejs-tutorial/ post.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)