Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f889b81

Browse files
committed
next version of post
1 parent d47f425 commit f889b81

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/cookbook/gradual-compositionapi-migration.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Gradual Composition API Migration
22

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 find 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? 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.
44

55
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.
66

@@ -122,21 +122,21 @@ And we can refactor our component as follows:
122122
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
123123
></iframe>
124124
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`, 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.
126126

127127
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.
128128

129129
## In Place of Vuex
130130

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.
132132

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.
134134

135135
## Components
136136

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.
138138

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.
140140

141141
For instance, if you have a component has the following methods:
142142

@@ -145,11 +145,13 @@ For instance, if you have a component has the following methods:
145145
- one that uses the input to call out to an API
146146
- one that does something with that API response
147147

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.
149151

150152
## Alternative Patterns
151153

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.
153155

154156
Remember: the strength of the Composition API is in:
155157

0 commit comments

Comments
 (0)