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/cookbook/gradual-compositionapi-migration.md
+10-8Lines changed: 10 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Gradual Composition API Migration
2
2
3
-
The Composition API is an advanced feature introduced in Vue 3. It is purely additive, the Options API is not legacy, however you may findthat the Composition API can be a useful feature for more flexible large-scale architectures. So how would we go about introducing it in a larger system, gradually? The following is merely a recommendation, there are many ways of going about this.
3
+
The [Composition API](/guide/composition-api-introduction.html) is an advanced feature introduced in Vue 3. It is purely additive, and the Options API is not legacy. You may find, however, that the Composition API can be a useful feature for more flexible large-scale architectures. So how would we go about introducing it in a larger system, gradually? What follows is merely a recommendation, there are many potential ways forward.
4
4
5
5
Due to the flexibility of the Composition API, you may need to be more explicit about organization so that future maintainers can quickly understand intended purpose of pieces of the application.
6
6
@@ -122,21 +122,21 @@ And we can refactor our component as follows:
Note that the template stays the same, but we've extracted the logic to use in our script section differently. If, from here, we wanted to use `isShowing` in the Options API, we could access it with `this.isShowing`, just as we normally do with a data property, and similarly, we can access `this.toggleShow` like we would a method.
125
+
Note that the template stays the same, but we've extracted the logic to use in our script section differently. If, from here, we wanted to use `isShowing` in the Options API, we could access it with `this.isShowing` now, just as we normally do with a data property, and similarly, we can access `this.toggleShow` like we would a method.
126
126
127
127
You may notice we used `reactive` here instead of `refs`. This is intentional- if you're refactoring a codebase, potentially full of many many values, `reactive` translates faster and more directly as the API is closer to Options, you're still using that same object notation.
128
128
129
129
## In Place of Vuex
130
130
131
-
It is possible to use the Composition API in place of Vuex, and save yourself a dependency. That said, it's not exactly necessary, either. And there are some tradeoffs.
131
+
It is possible to use the Composition API in place of Vuex, and save yourself a dependency. That said, it's not exactly necessary, and like many approaches, there are tradeoffs.
132
132
133
-
If you're using Vuex, it's very clear exactly what centralized state is being used across the application. Composition API is very flexible, but you may lose that implicit declaration in communication to other fellow maintainers. Our suggestion that if you do use it as a centralized state management store, that you place it in a `store` folder, or something similarly named, so that responsibilities are clear.
133
+
If you're using Vuex, it's very clear exactly what centralized state is being used across the application. Composition API is very flexible, but you may lose that implicit declaration in communication to fellow maintainers. Our suggestion that if you do use it as a centralized state management store, that you place it in a `store` folder, or something similarly named, so that responsibilities are clear.
134
134
135
135
## Components
136
136
137
-
It is possible at this point to start to move your components to the Composition API, however, for larger code bases, you may not see immediate need or good reason to, as the Options API still works nicely.
137
+
It is possible at this point to start to move your components to the Composition API, however, for larger code bases, you may not see immediate need or good reason to, as the Options API works nicely.
138
138
139
-
Our suggestion is to use this opportunity to instead refactor any larger components that have too much responsibility on their own.
139
+
Our suggestion is to use this opportunity to think through the architecture and refactor any larger components that have too much responsibility on their own.
140
140
141
141
For instance, if you have a component has the following methods:
142
142
@@ -145,11 +145,13 @@ For instance, if you have a component has the following methods:
145
145
- one that uses the input to call out to an API
146
146
- one that does something with that API response
147
147
148
-
You may find that the one component has a lot of responsibility, and it may make sense to break this apart more.
148
+
You may find that you can potentially create a reusable component for the API call, as on a large application there's a fairly high chance you will need to make another.
149
+
150
+
[VueUse](https://vueuse.js.org/) is a great resource to explore that covers many of these encapsulated use cases.
149
151
150
152
## Alternative Patterns
151
153
152
-
Not everything needs to be refactored, though. Use judgement to guide you and not hype. The Options API can support a lot of use cases, and it's absolutely fine to keep your components in the Options API, and move reusable logic and/or state management to Composition API, depending on your needs.
154
+
Not everything needs to be refactored, though. Use judgement to guide you and not hype. The Options API can support quite a lot of use cases, and it's absolutely fine to keep your components in the Options API, and move reusable logic and/or state management to Composition API, depending on your needs.
153
155
154
156
Remember: the strength of the Composition API is in:
0 commit comments