Watch

今天要介绍的是watch属性

Watch他跟computed最大的不同是他没有cache的机制,那watch是要做什麽的呢?
我们先来看看官网的介绍吧!

Vue does provide a more generic way to observe and react to data changes on a Vue instance: watch properties. When you have some data that needs to change based on some other data, it is tempting to overuse watch. However, it is often a better idea to use a computed property rather than an imperative watch callback.

While computed properties are more appropriate in most cases, there are times when a custom watcher is necessary. That’s why Vue provides a more generic way to react to data changes through the watch option. This is most useful when you want to perform asynchronous or expensive operations in response to changing data.

从官网的介绍中我们可以知道watch它可以观察和监听资料的变动,可是如果你是要做资料对其它资料的变动时使用computed会是比较好的选择,因为在这种情况下使用watch很容易导致我们过度使用,看到这里你可能会想说那我们就都使用computed就好啦,为什麽还要使用watch呢?从官网中的另一段也就是这里撷取的第二段它又告诉我们虽然computed在很多情况下都适用,但是当我们在执行非同步或是比较复杂的运算时,我们还是需要使用watch属性去监听资料的变动,所以在执行非同步或是资料一直改变需要一直监听的时候,使用watch就很合适

在今天的范例中还有使用到watch中的deep属性以及localStorage
deep属性是去做深度监听,也就是他会去监听每个参数,而localStorage它是可以将我们的资料储存在网页中,当使用者关闭浏览器在开启同一个浏览器时资料也都会存在喔!当你点开application会发现我们输入的资料会存在那里面
https://ithelp.ithome.com.tw/upload/images/20210915/20139183Rg9taZuCAg.jpg

Demo
github


<<:  [Day20] Laravel起步走

>>:  D6 - 你不知道 Combo : 主菜 Scope 字汇环境

Linux 解压缩 .zip 档案

下载压缩及解压缩工具 yum install zip unzip 将 data 目录下所有档案压储到...

Day 13. Hashicorp Vault: HA with Integrated Storage

Hashicorp Vault: HA with Integrated Storage 昨天有提到H...

EP20 - 整合 Jenkins 自动部署到 EKS

之前我们自动部署的时候, 没有在 Jenkins Server 上 config aws, 而是起一...

梳理useEffect和useLayoutEffect的原理与区别

点击进入React源码调试仓库。 React在构建用户界面整体遵循函数式的编程理念,即固定的输入有固...