QA: 状态管理的 getter 和 setter 如何使用 #1456
Unanswered
chilingling
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
概念
computed
(实际不完全等同,用的是 watchEffect 实现)用途:用于响应式计算多种状态组合
用途:用于监听状态的变化,并根据状态变化执行对应的逻辑
getter 举例:
我们现在有三个状态变量:
所以 fullName 我们可以这样设置:
解释:
这样子,一旦 firstName 和 lastName 有变化,我们的 fullName 就会自动跟随变化。
对应出码后的高代码:
setter 举例:
假设现在 lastName 和 firstName 变化之后,我们都需要向上 emit 一个事件。
那么,我们可以在 fullName 的 setter中这么写:
对应的出码后的高代码:
Beta Was this translation helpful? Give feedback.
All reactions