<stylescoped> /* Modify the code to bind the dynamic color */ p { color: v-bind(theme); } </style>
2.5 ref 全家桶
在这个挑战中,你将使用 响应式 API: ref 来完成它。 以下是你要实现的内容 👇:
<scriptsetuplang="ts"> import { ref, Ref, reactive, isRef, unref, toRef } from"vue"; const initial = ref(10); const count = ref(0); // Challenge 1: Update ref functionupdate(value) { 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 */ functioninitialCount(value: number | Ref<number>) { // Make the output be true // console.log(value === 10) 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); </script>
<scriptsetuplang="ts"> // Add a piece of code to make the `count` value get injected into the child component. import { inject } from"vue"; const count = inject("count"); </script>