博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Vue中父组件向子组件传递一个动态的值,子组件保持实时更新。
阅读量:4029 次
发布时间:2019-05-24

本文共 1327 字,大约阅读时间需要 4 分钟。

场景:父组件发生数据变化,动态的传递给子组件,子组件实时刷新视图

解决方法:需要在子组件watch中(监听)父组件数据的变化
在子组件中使用watch应该注意的问题:

1.watch监听普通类型的数据:

data() {
return {
frontPoints: 0 } }, watch: {
frontPoints(newValue, oldValue) {
console.log(newValue) } }

2.watch监听数组类型 的数据

data() {
return {
winChips: new Array(11).fill(0) } }, watch: {
  winChips: {
    handler(newValue, oldValue) {
      for (let i = 0; i < newValue.length; i++) {
        if (oldValue[i] != newValue[i]) {
          console.log(newValue)         }       }     },     deep: true   } }

3.watch监听对象类型的数据

data() {
  return {
    bet: {
      pokerState: 53,       pokerHistory: 'local'     } } }, watch: {
  bet: {
    handler(newValue, oldValue) {
      console.log(newValue)     },     deep: true   } }

4.watch监听对象的具体属性:(结合computed)

data() {
  return {
    bet: {
      pokerState: 53,       pokerHistory: 'local'     } } }, computed: {
  pokerHistory() {
    return this.bet.pokerHistory   } }, watch: {
  pokerHistory(newValue, oldValue) {
    console.log(newValue)   } }

tips: 只要bet中的属性发生变化(可被监测到的),便会执行handler函数;

如果想监测具体的属性变化,如pokerHistory变化时,才执行handler函数,则可以利用计算属性computed做中间层。

转载地址:http://nmlbi.baihongyu.com/

你可能感兴趣的文章
部分笔试算法题整理
查看>>
Ubuntu 13.10使用fcitx输入法
查看>>
pidgin-lwqq 安装
查看>>
mint/ubuntu安装搜狗输入法
查看>>
C++动态申请数组和参数传递问题
查看>>
opencv学习——在MFC中读取和显示图像
查看>>
retext出现Could not parse file contents, check if you have the necessary module installed解决方案
查看>>
pyQt不同窗体间的值传递(一)——对话框关闭时返回值给主窗口
查看>>
linux mint下使用外部SMTP(如网易yeah.net)发邮件
查看>>
北京联通华为光猫HG8346R破解改桥接
查看>>
python使用win32*模块模拟人工操作——城通网盘下载器(一)
查看>>
python append 与浅拷贝
查看>>
Matlab与CUDA C的混合编程配置出现的问题及解决方案
查看>>
2017阿里内推笔试题--算法工程师(运筹优化)
查看>>
python自动化工具之pywinauto(零)
查看>>
python一句话之利用文件对话框获取文件路径
查看>>
PaperDownloader——文献命名6起来
查看>>
PaperDownloader 1.5.1——更加人性化的文献下载命名解决方案
查看>>
如何将PaperDownloader下载的文献存放到任意位置
查看>>
C/C++中关于动态生成一维数组和二维数组的学习
查看>>