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&React] [2021-02-10 更新] Vue计算属性和普通属性的区别? #22

Open
jeddygong opened this issue Feb 10, 2021 · 1 comment
Labels

Comments

@jeddygong
Copy link
Owner

No description provided.

@jeddygong jeddygong added the vue label Feb 10, 2021
@jeddygong jeddygong changed the title [2021-02-10 更新] Vue计算属性和普通属性的区别? [Vue&React] [2021-02-10 更新] Vue计算属性和普通属性的区别? Feb 10, 2021
@jeddygong
Copy link
Owner Author

区别:

  • computed属性是vue的计算属性,是数据层到视图层的数据转化映射;
  • 计算属性是基于他们的依赖进行缓存的,只有在相关依赖发生改变时,他们才会重新求值,也就是说,只要他的依赖没有发生变化,那么每次访问的时候计算属性都会立即返回之前的计算结果,不再执行函数;
    • computed是响应式的,methods并非响应式。
    • 调用方式不一样,computed定义的成员像属性一样 访问,methods定 义的成员必须以函数形式调用。
    • computed是 带缓存的,只有依赖数据发生改变,才 会重新进行计算,而methods 里的函数在每次调用时都要执行。
    • computed中的成 员可以只定义一个函数作为只读属性,也可以定义get/set变成可读写属性,这点是methods中的成员做不到的
    • computed不 支持异步,当computed内有 异步操作时无效,无法监听数据的变化

如果声明的计算属性计算量非常大的时候,而且访问量次数非常多,改变的时机却很小,那就需要用到computed;缓存会让我们减少很多计算量

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant