当前位置:首页 > Vue watcher oldValue和newValue始终一样,watcher监听一个对象的具体属性

Vue watcher oldValue和newValue始终一样,watcher监听一个对象的具体属性

发布于 2018-08-28 阅读 3752 次 Javascript Vue

背景

写这篇文章源于我做的一个项目中需要实时监听一组输入框的合法与否。如下图所示:

如图这是一个图文列表,当切换左侧列表的时候后右侧需要显示对应的可编辑状态,产品的要求是,初始化的时候不能显示错误提示,在输入的时候在进行判断,添加一条新的图文的时候也需要进行判断,但是新添加的也不能显示错误信息,需要输入的时候实时显示。

我本来的解决方法是,

这些图文存在一个数组article里面,用watcher检测article里面的数据是否合法,然后实时显示错误信息,但是遇到一个问题,就是当监听整个article的时候只要里面一个属性发生变化,就会全部执行,这样没有过输入过的输入框的错误信息也被显示出来,这样显的和突兀。

而且在这个过程中我发现一个问题就是newValue 和 oldValue始终是一样的,查阅了相关资料才发现,

相关资料见 链接

解决新旧值始终相同的办法

我所用的办法其实就是克隆一个新的对象
每次在watcher里面将新值克隆一份存在vue的data之中

  1. watch:{
  2. articles:{
  3. handler:function(newValue, oldValue){
  4. this.oldValue = JSON.parse(JSON.stringify(newValue));
  5. //也可以采用其他方式,注意如果对象中有Date对象需要单独处理,则不可用上面这种方式
  6. },
  7. deep:true
  8. }
  9. }

配合计算属性watcher监听对象属性

所以就想到了要拆开检测,watcher是不能直接监听对象某个属性的变化的,我们需要借助计算属性配合;
相关代码:

  1. computed:{
  2. curTitle(){
  3. return this.curArticles.title;
  4. }
  5. },
  6. watch:{
  7. curTitle(newValue, oldValue){
  8. if(newValue == ''){
  9. this.error.title = '必填项不能为空';
  10. }else{
  11. this.error.title = '';
  12. }
  13. }
  14. }

采用这种方式后我仍然遇到了问题,因为我监听的是四个属性,但是点击添加一条新图文的时候时要避免watcher的,所以仍然需要做一定的处理,想必你也想到了,对,就是加一个开关,这里涉及的是四个属性分别watcher,所以一个开关变量用true,false显然不行了,所以我用的是数字。废话不多说,直接上完整代码片段:

  1. data:{
  2. //列表
  3. articles: [
  4. {
  5. "title":"",
  6. "description":"",
  7. "url":"",
  8. "picurl":""
  9. }
  10. ],
  11. //编辑的图文信息
  12. curArticles:{
  13. "title":"",
  14. "description":"",
  15. "url":"",
  16. "picurl":""
  17. },
  18. //错误提示
  19. error:{
  20. "title":"",
  21. "description":"",
  22. "url":"",
  23. "picurl":""
  24. },
  25. //用来判断是否刚新添加
  26. added: 0
  27. },
  28. computed:{
  29. curTitle(){
  30. //将新的title更新到article
  31. this.articles[this.cur].title = this.curArticles.title;
  32. return this.curArticles.title;
  33. },
  34. curDescription(){
  35. //将新的description更新到article
  36. this.articles[this.cur].description = this.curArticles.description;
  37. return this.curArticles.description;
  38. },
  39. curPicurl(){
  40. this.articles[this.cur].picurl = this.curArticles.picurl;
  41. return this.curArticles.picurl;
  42. },
  43. curUrl(){
  44. this.articles[this.cur].url = this.curArticles.url;
  45. return this.curArticles.url;
  46. }
  47. },
  48. watch:{
  49. curTitle(newValue, oldValue){
  50. if(this.added>0){
  51. this.added--;
  52. return;
  53. }
  54. if(newValue == ''){
  55. this.error.title = '必填项不能为空';
  56. }else{
  57. this.error.title = '';
  58. }
  59. },
  60. curDescription(newValue, oldValue){
  61. if(this.added>0){
  62. this.added--;
  63. return;
  64. }
  65. if(newValue == ''){
  66. this.error.description = '必填项不能为空';
  67. }else{
  68. this.error.description = '';
  69. }
  70. },
  71. curPicurl(newValue, oldValue){
  72. if(this.added>0){
  73. this.added--;
  74. return;
  75. }
  76. if(newValue == ''){
  77. this.error.picurl = '必填项不能为空';
  78. }else{
  79. this.error.picurl = '';
  80. }
  81. },
  82. curUrl(newValue,oldValue){
  83. if(this.added>0){
  84. this.added--;
  85. return;
  86. }
  87. var reg = new RegExp('(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]');
  88. if(newValue == ''){
  89. this.error.url = '必填项不能为空';
  90. }else if(!reg.test(newValue.trim())){
  91. this.error.url = 'URL地址错误';
  92. }else{
  93. this.error.url = '';
  94. }
  95. }
  96. },
  97. methods:{
  98. /**
  99. * 添加一条空消息
  100. * @method addArticle
  101. */
  102. addArticle(){
  103. var error = this.showError(this.cur);
  104. if(error) return;
  105. this.articles.push({
  106. "title":"",
  107. "description":"",
  108. "url":"",
  109. "picurl":""
  110. })
  111. this.cur = this.articles.length-1;
  112. this.added = 4;
  113. this.curArticles = JSON.parse(JSON.stringify(this.articles[this.cur]));
  114. },
  115. },
  116. mounted(){
  117. //在这里做一些初始化的工作,初始化articles和curArticles
  118. }

只是一些代码片段。仅供参考。