Promise Component for Vue 3 #11197
snqn
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello everyone,
This is a Promise-based component encapsulation method. Designed to simplify the handling of asynchronous input and output of components. Its design goal is to implement the software engineering concept of
High-cohesion and Low-coupling
.This component rendering mode has actually appeared in many UI frameworks (such as
MessageBox
,Modal
and other components), but it has never been extracted and used separately (or I haven't found it yet), so as to support any component to call in the form of Promise.Next, please see the introduction and examples. I look forward to getting more suggestions from everyone.
Features
🔥 Promise-based invocation
Promise-based invocation allows us to flexibly control the asynchronous input and output flow of components. The
component will be invoked internally at the appropriate time resolve or reject callback. This invocation follows a normalized pattern of asynchronous operation, making the use and management of components more reliable and consistent.
📦 Independence of calls
Each call to the component spawns a new, independent instance. They don't share call state, and they don't have issues like state caching. Whether the same component is called multiple times in a single page, or multiple instances of the same component are used in different pages, they are guaranteed to be independent of each other.
🙋 Render on demand
Components are rendered only when they are needed. This rendering method can be triggered according to specific events or external conditions, making the rendering logic more flexible and controllable. For example, we call a component when a user clicks a button or when a condition is met. This on-demand rendering method can effectively improve page load speed and performance, while also reducing unnecessary rendering and resource consumption.
♻️ Destroy after use
The result of a component's rendering is temporary and will be destroyed as soon as it is finished, similar to the effect of burning. This feature is ideal for temporary and one-off scenarios, while also improving program performance.
Installation
Example
Let's implement a user list and include the ability to interactively add and edit user information using a dialog box.
Initialization
You need to use the shared rendering slot of the Promise component in the root component, which will provide a default rendering location for the Promise components of the entire application and inheritance of the application context (such as: store, theme, i18n...).
Defining a Promise Component
Since the Promise component instance cannot be exported directly in the
.vue
file, an additional.ts
file is required to create the instance.File name suggestions:
index.ts
[name].promise.ts
Using the Promise component
Well, we have happily completed the development of the user list function based on the Promise component.
Custom Render Slots
Component slot
If you want to render a Promise component in a specific location, you can use the custom slot of the Promise component.
Interface
Github repository
Beta Was this translation helpful? Give feedback.
All reactions