Vue易混淆点

<!--父组件Parent-->
<template>
  <div>
    <group>
      <custom ref='child'></custom>
    </group>
  </div>
</template>

在组件内调用this.$refs.child返回VueComponent,是一个组件对象,就是组件<custom>的this。
在custom组件内,调用this.$parent,注意返回的是group的组件,而不是Parent组件。

<!--父组件Parent-->
<template>
  <div>
    <div>
        <div>  
          <custom ref='child'></custom>
        </div>
    </div>
  </div>
</template>

此时,在custom组件内,调用this.$parent,注意返回的是Parent的组件。

compute属性可以设置get/set属性

computed: {
  fullName: {
    // getter
    get: function () {
      return this.firstName + ' ' + this.lastName
    },
    // setter
    set: function (newValue) {
      var names = newValue.split(' ')
      this.firstName = names[0]
      this.lastName = names[names.length - 1]
    }
  }
}

现在再运行 vm.fullName = 'John Doe' 时,setter 会被调用,vm.firstName 和 vm.lastName 也会相应地被更新。

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容