laravel vue通过隐藏字段获取信息 [英] laravel vue getting info by hidden field
问题描述
我需要将登录的用户ID传递到后端,并且我拥有vuex存储,这样我才能获得 {{currentUser.id}}
这样的用户信息,问题是我无法将其传递回-end它给了我一个验证错误,即我在表单中有此隐藏输入时需要 user_id
I need to pass logged user id to back-end and I have vuex store so I can get my user info like {{currentUser.id}}
the problem is i cannot pass it to back-end it gives me validation error that user_id
is required while i have this hidden input in my form
<input type="hidden" name="user_id" :value="currentUser.id">
对于普通输入,我有 v-model
之类的 v-model ="project.title"
,无法在隐藏字段上使用.
for normal inputs i have v-model
like v-model="project.title"
which is not possible to use on hidden fields.
这里的问题是如何将我的
user_id
传递给后端?
The question here is how can I pass my
user_id
to back-end?
代码
<script>
import validate from 'validate.js';
export default {
data: function () {
return {
project: {
title: '',
body: '',
attachment: '',
projectclass: '',
deadline: '',
user_id: '',
csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
},
errors: null
}
},
computed: {
currentUser() {
return this.$store.getters.currentUser;
}
},
methods: {
add() {
this.errors = null;
const errors = validate(this.$data.project);
if(errors) {
this.errors = errors;
return;
}
axios.post('/api/projects/new', this.$data.project)
.then((response) => {
this.$router.push('/projects');
});
}
}
}
</script>
推荐答案
之所以会发生这种情况,是因为this.$ data.project中的user_id没有得到更新.
This happens because user_id in this.$data.project dosn't get updated.
除了隐藏输入之外,您还可以做
Instead of having hidden input you can just do
add() {
this.errors = null;
const errors = validate(Object.assign(this.$data.project, {user_id: this.currentUser.id}));
if(errors) {
this.errors = errors;
return;
}
axios.post('/api/projects/new', Object.assign(this.$data.project, {user_id: this.currentUser.id}))
.then((response) => {
this.$router.push('/projects');
});
}
这篇关于laravel vue通过隐藏字段获取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!