vuejs-challenges icon indicating copy to clipboard operation
vuejs-challenges copied to clipboard

2 - ref 全家桶

Open undefined-zzk opened this issue 1 year ago • 0 comments

<!-- 小贴士:

🎉 恭喜你成功解决了挑战,很高兴看到你愿意分享你的答案!

由于用户数量的增加,Issue 池可能会很快被答案填满。为了保证 Issue 讨论的效率,在提交 Issue 前,请利用搜索查看是否有其他人分享过类似的档案。

你可以为其点赞,或者在 Issue 下追加你的想法和评论。如果您认为自己有不同的解法,欢迎新开 Issue 进行讨论并分享你的解题思路!

谢谢! -->

// 你的答案

`

const initial = ref(10) const count = ref(0)

// Challenge 1: Update ref function update(value) { // impl... count.value=value }

/**

  • Challenge 2: Check if the count is a ref object.
  • Make the output be 1 */ console.log( // impl ? 1 : 0 isRef(count) ? 1:0 )

/**

  • Challenge 3: Unwrap ref
  • Make the output be true */ function initialCount(value: number | Ref) { // Make the output be true console.log(unref(value) === 10) }

initialCount(initial)

/**

  • Challenge 4:
  • create a ref for a property on a source reactive object.
  • The created ref is synced with its source property:
  • mutating the source property will update the ref, and vice-versa.
  • Make the output be true */ const state = reactive({ foo: 1, bar: 2, }) const fooRef = toRef(state,'foo') // change the impl...

// mutating the ref updates the original fooRef.value++ console.log(state.foo === 2)

// mutating the original also updates the ref state.foo++ console.log(fooRef.value === 3)

`

undefined-zzk avatar Jan 30 '24 09:01 undefined-zzk