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/composition-api-setup.md
+7-20Lines changed: 7 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -57,10 +57,10 @@ The second argument passed to the `setup` function is the `context`. The `contex
57
57
58
58
exportdefault {
59
59
setup(props, context) {
60
-
// Attributes (Reactive Property)
60
+
// Attributes (Non-reactive object)
61
61
console.log(context.attrs)
62
62
63
-
// Slots (Reactive Property)
63
+
// Slots (Non-reactive object)
64
64
console.log(context.slots)
65
65
66
66
// Emit Events (Method)
@@ -69,31 +69,18 @@ export default {
69
69
}
70
70
```
71
71
72
-
Because it is a normal JavaScript object, i.e., it is not reactive, this means you can safely use ES6 destructuring on `context`.
72
+
The `context` object is a normal JavaScript object, i.e., it is not reactive, this means you can safely use ES6 destructuring on `context`.
73
73
74
74
```js
75
75
// MyBook.vue
76
76
exportdefault {
77
-
setup(props, { attrs, slots, emit }) {
78
-
...
79
-
}
77
+
setup(props, { attrs, slots, emit }) {
78
+
...
79
+
}
80
80
}
81
81
```
82
82
83
-
As a result, similar to `props`, if you need to destructure either of these properties, you can utilize the `toRefs` method to create a similar effects.
84
-
85
-
```jsx
86
-
// MyBook.vue
87
-
88
-
import { toRefs } from'vue'
89
-
90
-
exportdefault {
91
-
setup(props, { attrs }) {
92
-
const { id } =toRefs(attrs)
93
-
console.log(id.value)
94
-
}
95
-
)
96
-
```
83
+
`attrs` and `slots` are stateful objects that are always updated when the component itself is updated. This means you should avoid destructuring them and always reference properties as `attrs.x` or `slots.x`. Also note that unlike `props`, `attrs` and `slots` are **not** reactive. If you intend to apply side effects based on `attrs` or `slots` changes, you should do so inside an `onUpdated` lifecycle hook.
0 commit comments