Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue 组件相关 #69

Open
shinena opened this issue Oct 11, 2023 · 1 comment
Open

Vue 组件相关 #69

shinena opened this issue Oct 11, 2023 · 1 comment

Comments

@shinena
Copy link
Owner

shinena commented Oct 11, 2023

单向数据流

所有的prop都使得其父子之间形成了一个单向下行绑定:父级prop的更新会向下流动到子组件中,但是反过来则不行。
防止从子组件意外变更父级组件的状态,从而导致应用中的数据流难以理解。

每次父级组件发生变更时,子组件中所有的prop都将会刷新为最新的值。不能在一个子组件内部改变prop

两种常见的试图变更一个prop的情形:

  1. 这个prop用来传递一个初始值;这个子组件接下来希望将其作为一个本地的prop数据来使用。这种情况下,最好定义一个本地的data property并将这个prop用作其初始值:
props: ['initialCounter'],
data: function () {
  return {
    counter: this.initialCounter
  }
}
  1. 这个prop以一种原始的值传入且需要进行转换。在这种情况下,最好使用这个prop的值来定义一个计算属性:
props: ['size'],
computed: {
  normalizedSize: function () {
    return this.size.trim().toLowerCase()
  }
}
@shinena
Copy link
Owner Author

shinena commented Oct 11, 2023

data中的属性为什么是一个function?

一个组件的data选项必须是一个函数,因此每个实例可以维护一份被返回对象的独立的拷贝

@shinena shinena changed the title Vue 单向数据流 Vue 组件相关 Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant