一、原因:一个计算属性,当计算传入的是一个函数,或者传入的是一个对象,而没有设置 setter,也就是 set 属性,当你尝试直接该改变这个这个计算属性的值,都会报这个警告,vuex还会出现通过commit提交修改vuex值的警告的情况。

参考文档:

二、常见导致该错误的写法

(1)计算属性中传入的是对象和方法,直接对计算属性进行赋值会导致错误

比如:

<template>
<div>
<input v-model='change'/>
</div>
</template> <script>
export default {
data () {
return {
a: ''
}
},
computed: {
change () {
return this.a
}
}
}
</script>

结合vuex比如:

<template>
<div>
<input v-model='change'/>
</div>
</template> <script>
export default {
data () {
return {
}
},
computed: {
change () {
return this.$store.state.val
}
}
}
</script>
<template>
<div>
<input v-model='change'/>
</div>
</template> <script>
export default {
data () {
return {
a: ''
}
},
computed: {
...mapState({
// 获取vuex中某个对象的属性值给页面展示用,页面双向绑定也会更新计算属性
change: state => state.object.change
})
}
}
</script>
<template>
<div>
<div @click='change()'>点我</div>
</div>
</template> <script>
export default {
data () {
return { }
},
computed: {
...mapState({
// 获取vuex中某个对象的属性值给页面展示用
change: state => state.object.change
})
},
method: {
change () {
this.change = '赋值'
}
}
}
</script>

三、解决方法

(1)将计算属性转成一个对象,使用getter与setter

   <div>
<input v-model='change'/>
</div>
</template> <script>
export default {
data () {
return {
a: ''
}
},
computed: {
//change () {
// return this.a
//} change :{ // getter 将值赋给change
get: function () {
return this.a
},
// setter 获取改变后的值并设置给a
set: function (newValue) {
this.a = newValue;
}
}
}
}
</script>

(2)vuex获取所需对象而不是具体的属性值,后面进行赋值的操作也会更改vuex中的值(对象按值传递)

<template>
<div>
<!--双向绑定会改变vuex中change的值,官方建议通过commit的方式改变vuex值-->
<input v-model='object.change'>
<div @click='change()'>点我</div>
</div>
</template> <script>
export default {
data () {
return { }
},
computed: {
...mapState({
// 获取vuex中某个对象的属性值给页面展示用
//change: state => state.object.change
object: state => state.object
})
},
method: {
change () {
this.object.change = '赋值' // 同步修改了vuex值
this.change = this.object.change
}
}
}
</script>

 扩展:

一、vue报错Error in mounted hook: "TypeError: handlers[i].call is not a function"

原因:钩子函数书写错误或者钩子函数中使用了未定义的方法

vue报类似警告Computed property "isLoading" was assigned to but it has no setter的更多相关文章

  1. vue store获取值时 Computed property "activeTag" was assigned to but it has no setter.

    出现原因: element-ui中 el-tab绑定的值在切换tab时会自动修改 而activeTag是从store中获取的值,不能直接修改 要添加给它绑定上set   <el-tabs cla ...

  2. vue 报错 Cannot read property '__ob__' of undefined的解决方法

    记不清第n次遇到这个错误了,但是脑子就是不好用,记不住解决办法啊,每次都要找好久才能找到错误,网上还一篇篇的全是错误答案......所以写篇随笔,记录下,方便大家也方便我自己. 网上有人说是组件循环了 ...

  3. Vue报错Cannot read property 'split' of undefined

    今天在项目中处理后端返回的字符串需要使用split做一个字符串转数组的处理,之前项目都运行得好好的,今天突然出问题了,然后面向百度编程了一波,如果你也是用的异步向后端发送请求,可能你的问题和我一样,继 ...

  4. Vue——解决报错 Computed property "****" was assigned to but it has no setter.

    在最近的项目中遇到了如下的警告信息: [Vue warn]: Computed property " currentStep" was assigned to but it has ...

  5. vue报错信息

    1.Property or method "xxx" is not defined on the instance but referenced during render. 原因 ...

  6. vue 源码解析computed

    计算属性 VS 侦听属性 Vue 的组件对象支持了计算属性 computed 和侦听属性 watch 2 个选项,很多同学不了解什么时候该用 computed 什么时候该用 watch.先不回答这个问 ...

  7. vue中watch和computed为什么能监听到数据的改变以及不同之处

    先来个流程图,水平有限,凑活看吧-_-|| 首先在创建一个Vue应用时: var app = new Vue({ el: '#app', data: { message: 'Hello Vue!' } ...

  8. 浅谈Vue中计算属性computed的实现原理

    虽然目前的技术栈已由Vue转到了React,但从之前使用Vue开发的多个项目实际经历来看还是非常愉悦的,Vue文档清晰规范,api设计简洁高效,对前端开发人员友好,上手快,甚至个人认为在很多场景使用V ...

  9. vue报错 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's

    [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent c ...

随机推荐

  1. python图论包networks(最短路,最小生成树带包)

    官方文档: https://networkx.github.io/documentation/networkx-1.10/reference/algorithms.html 最短路和最小生成树: im ...

  2. pod install报错 [!] Error installing......

    今天pod install出现这个错误: 解决办法: 多试几次就好了,也不知道之前几次都失败.....希望知道的可以留言告诉我哟!!!

  3. 【leetcode】520. Detect Capital

    problem 520. Detect Capital 题意: 题目中给出的三种情况,分别是全是大写.全是小写.首字母大写,这三种情况返回True;否则返回False; solution: class ...

  4. 超详细的EM算法理解

    众所周知,极大似然估计是一种应用很广泛的参数估计方法.例如我手头有一些东北人的身高的数据,又知道身高的概率模型是高斯分布,那么利用极大化似然函数的方法可以估计出高斯分布的两个参数,均值和方差.这个方法 ...

  5. 分布式消息通信之RabbitMQ_01

    目录 官网 1. RabbitMQ安装 1.1 Window版安装 1.2 Linux版安装 2. 典型应用场景 3. 基本介绍 3.1 AMQP协议 3.2 RabbitMQ的特性 3.3 工作模型 ...

  6. 在linux服务器以及客户端实现公钥免密登录

    每次登录服务器都要输入密码,这点比较麻烦.使用ssh公钥登录机制可以直接登录,避免每次都输入密码的烦恼. 所谓ssh公钥登录机制即是:客户端电脑client产生加密用的公钥id_rsa.pub与私钥i ...

  7. Fastjson反序列化漏洞

    payload: 1.{"@type":"com.sun.rowset.JdbcRowSetImpl","dataSourceName":& ...

  8. ActiveMQ单机部署及简单应用

    系统版本:Centos 7 前言 MQ是消息中间件,是一种在分布式系统中应用程序借以传递消息的媒介,常用的有ActiveMQ,RabbitMQ,kafka.ActiveMQ是Apache下的开源项目, ...

  9. js复制文本

    第一种: 自己测试时 只适合于input 和textarea 但是针对于其他标签的复制就不能用了.代码如下: <!DOCTYPE html> <html> <head&g ...

  10. PTA(Advanced Level)1044.Shopping in Mars

    Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...