You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/essentials/reactivity-fundamentals.md
+7-7
Original file line number
Diff line number
Diff line change
@@ -73,15 +73,15 @@ When you access `this.someObject` after assigning it, the value is a reactive pr
73
73
74
74
In Composition API, the recommended way to declare reactive state is using the [`ref()`](/api/reactivity-core#ref) function:
75
75
76
-
```js
76
+
```js twoslash
77
77
import { ref } from'vue'
78
78
79
79
constcount=ref(0)
80
80
```
81
81
82
82
`ref()` takes the argument and returns it wrapped within a ref object with a `.value` property:
83
83
84
-
```js
84
+
```js twoslash
85
85
constcount=ref(0)
86
86
87
87
console.log(count) // { value: 0 }
@@ -95,7 +95,7 @@ console.log(count.value) // 1
95
95
96
96
To access refs in a component's template, declare and return them from a component's `setup()` function:
97
97
98
-
```js{5,9-11}
98
+
```js{5,9-11} twoslash
99
99
import { ref } from 'vue'
100
100
101
101
export default {
@@ -127,7 +127,7 @@ You can also mutate a ref directly in event handlers:
127
127
128
128
For more complex logic, we can declare functions that mutate refs in the same scope and expose them as methods alongside the state:
129
129
130
-
```js{7-10,15}
130
+
```js{7-10,15} twoslash
131
131
import { ref } from 'vue'
132
132
133
133
export default {
@@ -150,7 +150,7 @@ export default {
150
150
151
151
Exposed methods can then be used as event handlers:
152
152
153
-
```vue-html{1}
153
+
```vue{1}
154
154
<button @click="increment">
155
155
{{ count }}
156
156
</button>
@@ -162,7 +162,7 @@ Here's the example live on [Codepen](https://codepen.io/vuejs-examples/pen/WNYba
162
162
163
163
Manually exposing state and methods via `setup()` can be verbose. Luckily, it can be avoided when using [Single-File Components (SFCs)](/guide/scaling-up/sfc). We can simplify the usage with `<script setup>`:
164
164
165
-
```vue{1}
165
+
```vue{1} twoslash
166
166
<script setup>
167
167
import { ref } from 'vue'
168
168
@@ -305,7 +305,7 @@ Refs can hold any value type, including deeply nested objects, arrays, or JavaSc
305
305
306
306
A ref will make its value deeply reactive. This means you can expect changes to be detected even when you mutate nested objects or arrays:
0 commit comments