:你应该只使用V模型和刚刚成立userInfo.name = user.name
创建组件时。就像Fatman在他的答案v模型中所说的覆盖价值属性。
这是我的建议,你应该怎么做编辑。我写得很快,所以它不完美。学习vuex并使其更好。
User.vue
<p>{{user.firstName}}</p>
<p>{{user.lastName}}</p>
<router-link :to="'/users/edit-user/'+user.rid"><a >Edit user</a></router-link>
EditUser.vue
<input type="text" v-model="user.firstName">
<input type="text" v-model="user.lastName">
computed: {
user() {
return clone(this.$store.state.currentUser);
}
},
methods: {
getTheUser() {
axios.get("/user/" + this.$route.params.id)
.then((response) => {
//this.user = response.data;
this.$store.commit('setCurrentUser', response.data);
})
.catch(function (res) {
this.$store.commit('clearCurrentUser');
}); },
updateUser() {
// check how you get the token and replace it down here
axios.put('/user/' + this.$route.params.id, this.user,
{'headers':{'X-AUTH-TOKEN':localStorage.token}},
{'headers':{'Content-Type': 'application/json'}})
.then((response) => {
this.$store.commit('setCurrentUser', this.user);
})
.catch((response) => {
console.log('error answer', response)
})
}
}
store.js
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export default new Vuex.Store({
state: {
currentUser: {}
},
mutations: {
setCurrentUser: function (state, users) {
state.currentUser = users;
},
clearCurrentUser: function (state) {
state.currentUser = {};
},
}
})
在main.js
import store from './store.js';
new Vue({
el: '#app',
store,
template: '<App/>',
components: { App }
})
确定我理解,但如果把你在我的代码中说的话,编辑页面中的输入仍然是空的,即使在我看到'users/edit-user/48'的url中。我在数据中初始化用户,并将其设置为带有特定用户的方法:'''getUsers(){$ http.get('/ user /'+ this。$ route.params.id) 。那么((响应)=> this.user =响应。数据; }) }''' –
和用户信息的数据是相同的:用户信息:{ 姓: '', 名字: '', 电子邮件: '', 密码: '', 电话: '' , idCard: '', DepartmentID的:1, 功能: '', JOBTITLE: '', cityId: '', countryId: '', userTypeId:1 }和在put方法使用:Axios公司.put('/ user /'+ this。$ route.params.id,this.userInfo) –
请发布组件,以便我可以看到你在做什么 – Tomer