【发布时间】:2020-08-13 17:27:07
【问题描述】:
我有一个很奇怪的问题,因为在开发电子应用程序时,这个问题没有出现在 Quasar 中。但是这个有 vue-cli 的就让人沮丧了。
所以我有 vuex 状态,我有我的测试数据
export default function() {
return {
profile: {
avatar: null,
name: 'Thomas Smith',
contact: 'thomas.smith@bootiq.io',
branch: 'BIQ-PRAGUE',
position: 'Software Developer / UI Designer',
projects: [
{
company: 'Company',
project: 'E-Shop Platform',
role: 'Frontend Developer, Consultant'
},
{
company: 'BIQ-INTERNAL',
project: 'Skillboard',
role: 'UI Design, Project Lead'
}
],
interests: ['Frontend Applications', 'UI Design', 'Stalking random people'],
skills: [
{
name: 'Javascript',
level: 2
},
{
name: 'Vue',
level: 3
},
{
name: 'Angular',
level: 1
}
]
}
}
}
现在我有了用于编辑个人资料数据的组件
computed: {
...mapState({
stateSkillList: state => state.skill.skillList
}),
...mapGetters({
user: 'user/getUser'
})
},
mounted() {
if (this.user) {
this.data = Object.assign({}, this.data, this.user)
if (this.data.projects.length === 0) {
this.data.projects = [this.baseItems.projects]
}
if (this.data.skills.length) {
this.filterSkillList()
}
}
},
现在我的问题。我有输入,其中 v-model 是 data.name、data.contact 等,一切正常。但是当我尝试从 data.skill 数组或 data.projects、data.interests 添加/删除技能时,我得到了
vue.esm.js?a026:628 [Vue warn]: Error in callback for watcher "function () { return this._data.$$state }": "Error: [vuex] do not mutate vuex store state outside mutation handlers."
(found in <Root>)
warn @ vue.esm.js?a026:628
logError @ vue.esm.js?a026:1893
globalHandleError @ vue.esm.js?a026:1888
handleError @ vue.esm.js?a026:1848
run @ vue.esm.js?a026:4579
update @ vue.esm.js?a026:4551
notify @ vue.esm.js?a026:739
mutator @ vue.esm.js?a026:891
handleRemoveSkill @ EditProfile.vue?05c4:306
click @ EditProfile.vue?0ae8:411
invokeWithErrorHandling @ vue.esm.js?a026:1863
invoker @ vue.esm.js?a026:2188
original._wrapper @ vue.esm.js?a026:7565
vue.esm.js?a026:1897 Error: [vuex] do not mutate vuex store state outside mutation handlers.
at assert (vuex.esm.js?2f62:90)
at Vue.store._vm.$watch.deep (vuex.esm.js?2f62:814)
at Watcher.run (vue.esm.js?a026:4577)
at Watcher.update (vue.esm.js?a026:4551)
at Dep.notify (vue.esm.js?a026:739)
at Array.mutator (vue.esm.js?a026:891)
at VueComponent.handleRemoveSkill (EditProfile.vue?05c4:306)
at click (eval at ./node_modules/cache-loader/dist/cjs.js?{"cacheDirectory":"node_modules/.cache/vue-loader","cacheIdentifier":"6bb1df4e-vue-loader-template"}!./node_modules/vue-loader/lib/loaders/templateLoader.js?!./node_modules/vuetify-loader/lib/loader.js?!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/views/EditProfile.vue?vue&type=template&id=7b259475&scoped=true& (app.js:1178), <anonymous>:414:44)
at invokeWithErrorHandling (vue.esm.js?a026:1863)
at HTMLButtonElement.invoker (vue.esm.js?a026:2188)
我不知道为什么。这是简单的功能
handleAddSkill(name) {
this.data.skills.push({ name })
this.dataSkillFilteredList = this.dataSkillFilteredList.filter(item => item !== name)
this.dataSkillList = this.dataSkillFilteredList
},
感谢您的意见
【问题讨论】:
-
没有minimal reproducible example 就无法回答您的问题,而且您迄今为止发布的内容不足以创建一个。如果您需要多文件节点环境,请使用 codesandbox.io 或类似的。