0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Vue.js] どうしてVue.set()で配列の更新が検知できるの?

Last updated at Posted at 2020-02-07

[Vue.js]配列の変更が検知できない理由の「JavaScriptの制限」って何よ! - Qiita

こちらの記事で配列の更新検知にはVue.setを使うと記載したのですが、
どうして検知できるのかな?って思って調べました。

結論

内部ではsplice()をしているだけだった。

実装を見てみる

公式GitHubを見てみましょう! :eye: :eye:

export function set (target: Array<any> | Object, key: any, val: any): any {
// 中略
  if (Array.isArray(target) && isValidArrayIndex(key)) {
     target.length = Math.max(target.length, key) 
     target.splice(key, 1, val) //ここだ!
     return val 
  } 
...

ありました!
内部でtarget.splice(key, 1, val)をしていることが確認できました!
だから検知できるんですね!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?