A method and device for customizing and dynamically rendering Vue components
By customizing the templates, JavaScript behavior, and styles of Vue components, the problem of template component dependency in existing technologies is solved, enabling flexible and rich dynamic rendering of Vue components and supporting component expansion during project runtime.
Patent Information
- Application Number
- CN202411180774.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-27
- Publication Date
- 2024-11-15
- Estimated Expiration
- 2044-08-27
AI Technical Summary
Existing Vue component dynamic rendering technology relies on pre-developed template components, which limits the flexibility of content.
By customizing the template structure, JavaScript behavior, and styles of Vue components, and processing templates, scripts, and styles using configuration data, custom Vue components can be generated, and custom development of components can be achieved through dynamic rendering.
It enhances the flexibility and richness of Vue components, eliminates the dependence on built-in template components, and enables component extension development during project runtime.
Smart Images

Figure CN118689472B_ABST
Abstract
Description
Technical Field
[0001] The present invention discloses a method and device for Vue component customization and dynamic rendering, and relates to the technical field of front-end development. Background Art
[0002] Vue component dynamic rendering is a method to improve the richness and flexibility of page content and reduce development costs. Existing Vue component dynamic rendering technologies are implemented based on configuration and the developed template components built into the system. The template components are filled with configuration and finally rendered. This method requires relying on pre-developed template components, which has a great limitation on content flexibility. Summary of the invention
[0003] In view of the problems of the prior art, the present invention provides a method and device for customizing and dynamically rendering a Vue component, which gets rid of the limitations of template components, uses configuration to perform custom development of Vue components, and greatly improves the flexibility and richness of component content.
[0004] The specific scheme proposed by the present invention is:
[0005] The present invention provides a method for customizing and dynamically rendering a Vue component, comprising:
[0006] Step 1: Customize the three components of the Vue component according to user needs to obtain configuration data. The three components are the component's template structure template, the component's JavaScript behavior script, and the component's style style.
[0007] Step 2: According to the configuration data, process template, script and style to form a custom Vue component:
[0008] Process the configuration data of the template, including: wrapping the div component on the outermost side of the template configuration content, and setting a unique identifier for the outermost div component for selection by the CSS selector.
[0009] Processing script configuration data, including: processing the Import module and logic script involved in the script in turn, wherein the dependent module name is parsed in the configuration of the Import module, the corresponding dependent module is obtained from the dependent module summary file according to the dependent module name, and the dependent modules are uniformly stored in an object modules,
[0010] When processing the logic script, the dependent module is obtained through the Import module, the definition code of the dependent module is generated according to the fixed format, the definition code of the dependent module is spliced at the beginning of the logic script, the final code string is obtained, and the code string is converted into an executable function using the newFunction() statement, and the executable function is executed to obtain the configuration items of the Vue component.
[0011] Generate a Vue component instance based on the processing results of the template and the configuration items obtained from the script processing, and render the Vue component instance;
[0012] Then process the style configuration data into native CSS code, add the CSS selector of the outermost div component of template before each CSS statement in the native CSS code to limit the scope of the CSS statement, create a style tag to store the native CSS code, insert the style tag into the DOM, and complete the processing of the CSS content in the Vue component instance.
[0013] Further, in step 2 of the method for customizing and dynamically rendering a vue component, executing an executable function to obtain configuration items of the vue component includes:
[0014] The executable function is denoted as fn, and fn is executed through the fn.apply(modules) statement of the apply method, and the execution context is specified as the object modules to obtain the configuration items of the vue component.
[0015] Furthermore, in step 2 of the method for customizing and dynamically rendering a vue component, the vue framework is assembled in a fixed format to generate the configuration object required for the vue component instance, and the content of the configuration object is rendered using the vue built-in component component to achieve the rendering of the vue component instance.
[0016] The present invention also provides a device for customizing and dynamically rendering a Vue component, including a configuration management module and a processing module.
[0017] The configuration management module customizes the three components of the Vue component according to user needs to obtain configuration data. The three components are the component's template structure template, the component's JavaScript behavior script, and the component's style style.
[0018] The processing module processes template, script and style according to the configuration data to form a custom Vue component:
[0019] Process the configuration data of the template, including: wrapping the div component on the outermost side of the template configuration content, and setting a unique identifier for the outermost div component for selection by the CSS selector.
[0020] Processing script configuration data, including: processing the Import module and logic script involved in the script in turn, wherein the dependent module name is parsed in the configuration of the Import module, the corresponding dependent module is obtained from the dependent module summary file according to the dependent module name, and the dependent modules are uniformly stored in an object modules,
[0021] When processing the logic script, the dependent module is obtained through the Import module, the definition code of the dependent module is generated according to the fixed format, the definition code of the dependent module is spliced at the beginning of the logic script, the final code string is obtained, and the code string is converted into an executable function using the newFunction() statement, and the executable function is executed to obtain the configuration items of the Vue component.
[0022] Generate a Vue component instance based on the processing results of the template and the configuration items obtained from the script processing, and render the Vue component instance;
[0023] Then process the style configuration data into native CSS code, add the CSS selector of the outermost div component of template before each CSS statement in the native CSS code to limit the scope of the CSS statement, create a style tag to store the native CSS code, insert the style tag into the DOM, and complete the processing of the CSS content in the Vue component instance.
[0024] Furthermore, the processing module of the device for customizing and dynamically rendering a vue component executes an executable function to obtain a configuration item of the vue component, including:
[0025] The executable function is denoted as fn, and fn is executed through the fn.apply(modules) statement of the apply method. The execution context is specified as the object modules to obtain the configuration items of the vue component.
[0026] Furthermore, the processing module of the device for customizing and dynamically rendering a vue component assembles the vue framework in a fixed format to generate the configuration object required for the vue component instance, and uses the vue built-in component component to render the content of the configuration object to achieve the rendering of the vue component instance.
[0027] The benefits of the present invention are:
[0028] The present invention eliminates the dependency of dynamic rendering of Vue components on the system's built-in template components, and can dynamically render components using only user-configured data, effectively improving the richness and flexibility of page content. The single-file component development of Vue is upgraded from existing project coding development to configuration-based development, so that the development work is not only limited to the project coding period, but also can expand and develop new component content during the project operation period. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] Figure 1 It is a schematic flow chart of the method of the present invention. DETAILED DESCRIPTION
[0030] DOM stands for Document Object Model.
[0031] The present invention is further described below in conjunction with the accompanying drawings and specific embodiments so that those skilled in the art can better understand the present invention and implement it, but the embodiments are not intended to limit the present invention.
[0032] Embodiment 1: The present invention provides a method for customizing and dynamically rendering a Vue component, comprising:
[0033] Step 1: Customize the three components of the vue component according to user needs and obtain configuration data. The three components are the component's template structure template, the component's JavaScript behavior script, and the component's style style. The Template configuration defines the page rendering content in the vue component and supports all syntax of the vue template definition part. The Script configuration is the script content of the vue component, which is divided into two parts: the import module and the logic script. The Import module configuration is used to introduce the dependent modules required by the logic script to replace the import statement; the logic script is the processing logic of the script part of the vue component except the import statement, and finally needs to return a configuration item required to create a vue component instance. The Style configuration is used in the vue component to customize the internal style of the component.
[0034] Step 2: According to the configuration data, process template, script and style to form a custom Vue component:
[0035] Process the configuration data of the template, including: wrapping the div component on the outermost side of the template configuration content to ensure that the template content is only under one root component to meet the requirements of the Vue framework, and setting a unique identifier for the outermost div component to be selected by the CSS selector to facilitate the limitation of the component style scope. The final template content is recorded as tplStr.
[0036] Processing script configuration data, including: processing the Import modules and logic scripts involved in the script in turn.
[0037] The dependent module name is parsed in the configuration of the Import module, and the corresponding dependent module is obtained from the dependent module summary file according to the dependent module name, and the dependent modules are uniformly stored in an object modules. The dependent module summary file provides an import interface for all configurable dependent modules of the Import module, and stipulates that the corresponding dependent module is obtained from the dependent module summary file through the dependent module name. The dependent module summary file can be recorded as moduleApi.js, and the export keyword can be used to selectively open the import interface moduleApi to the outside world, which can simplify the writing of import statements and control the access rights of external users to internal data.
[0038] When processing logic scripts, get the dependent modules through the Import module, generate the definition code of the dependent modules according to a fixed format, concatenate the definition code of the dependent modules at the front of the logic script, get the final code string, use the newFunction() statement to convert the code string into an executable function, and execute the executable function to get the configuration items of the Vue component.
[0039] In addition, when obtaining dependent modules according to dependent module names, if multiple dependent module names are separated by English commas, and when the definition code of the dependent module is generated according to a fixed format, the definition code can be recorded as preCode, and the fixed format can be referred to as follows: letmoduleA=this.moduleA;letmoduleB=this.moduleB;...
[0040] The moduleA and moduleB are the names of dependent modules; preCode is concatenated in front of the logic script to obtain the final usable code string, recorded as finalCode; the code string is converted into an executable function using the new Function() statement, and the executable function is recorded as fn. fn is executed through the fn.apply(modules) statement of the apply method, and the execution context is specified as the object modules to obtain the configuration item of the vue component, recorded as vueOpt.
[0041] Generate a vue component instance based on the processing result of the template and the configuration items obtained from the script processing, and render the vue component instance. The vue framework is assembled in a fixed format to generate the configuration object required for the vue component instance, recorded as vm, and the content of the configuration object is rendered using the vue built-in component component to achieve the rendering of the vue component instance. The fixed format can be referred to as follows:
[0042] {
[0043] template: tplStr,
[0044] ...vueOpt
[0045] }
[0046] The content of vm can be rendered using vue's built-in component component. The specific configuration method is: <component :is="”vm”">< / component> . That is, configure the is property to vm to implement component instance rendering.
[0047] Then process the style configuration data into native CSS code, add the CSS selector of the outermost div component of template before each CSS statement in the native CSS code to limit the scope of the CSS statement, create a style tag to store the native CSS code, insert the style tag into the DOM, and complete the processing of the CSS content in the Vue component instance.
[0048] Embodiment 2: The present invention also provides a device for customizing and dynamically rendering a vue component, comprising a configuration management module and a processing module.
[0049] The configuration management module customizes the three components of the Vue component according to user needs to obtain configuration data. The three components are the component's template structure template, the component's JavaScript behavior script, and the component's style style.
[0050] The processing module processes template, script and style according to the configuration data to form a custom Vue component:
[0051] Process the configuration data of the template, including: wrapping the div component on the outermost side of the template configuration content, and setting a unique identifier for the outermost div component for selection by the CSS selector.
[0052] Processing script configuration data, including: processing the Import module and logic script involved in the script in turn, wherein the dependent module name is parsed in the configuration of the Import module, and the corresponding dependent module is obtained from the dependent module summary file according to the dependent module name, and the dependent modules are uniformly stored in an object modules.
[0053] When processing the logic script, the dependent module is obtained through the Import module, the definition code of the dependent module is generated according to the fixed format, the definition code of the dependent module is spliced at the beginning of the logic script, the final code string is obtained, and the code string is converted into an executable function using the newFunction() statement, and the executable function is executed to obtain the configuration items of the Vue component.
[0054] Generate a Vue component instance based on the processing results of the template and the configuration items obtained from the script processing, and render the Vue component instance;
[0055] Then process the style configuration data into native CSS code, add the CSS selector of the outermost div component of template before each CSS statement in the native CSS code to limit the scope of the CSS statement, create a style tag to store the native CSS code, insert the style tag into the DOM, and complete the processing of the CSS content in the Vue component instance.
[0056] As the information interaction and execution process between the modules in the above-mentioned device are based on the same concept as the embodiment of the method of the present invention, the specific contents can be found in the description of the embodiment of the method of the present invention and will not be repeated here.
[0057] Similarly, the device of the present invention eliminates the dependence of the dynamic rendering of the vue component on the system's built-in template component, and can achieve dynamic rendering of components using only user-configured data, effectively improving the richness and flexibility of page content. The single-file component development of vue is upgraded from the existing project coding development to configuration development, so that the development work is not only limited to the project coding period, but also can expand the development of new component content during the project operation period.
[0058] It should be noted that not all steps and modules in the above-mentioned processes and device structures are necessary, and some steps or modules can be ignored according to actual needs. The execution order of each step is not fixed and can be adjusted as needed. The system structure described in the above-mentioned embodiments can be a physical structure or a logical structure, that is, some modules may be implemented by the same physical entity, or some modules may be implemented by multiple physical entities, or some components in multiple independent devices may be implemented together.
[0059] The above-described embodiments are only preferred embodiments for fully illustrating the present invention, and the protection scope of the present invention is not limited thereto. Equivalent substitutions or changes made by those skilled in the art based on the present invention are within the protection scope of the present invention. The protection scope of the present invention shall be subject to the claims.
Claims
1. A method for customizing and dynamically rendering a Vue component, characterized by include: Step 1: Customize the three components of the Vue component according to user needs to obtain configuration data. The three components are the component's template structure template, the component's JavaScript behavior script, and the component's style style. Step 2: According to the configuration data, process template, script and style to form a custom Vue component: Process the configuration data of the template, including: wrapping the div component on the outermost side of the template configuration content, and setting a unique identifier for the outermost div component for selection by the CSS selector. Processing script configuration data, including: processing the Import module and logic script involved in the script in turn, wherein the dependent module name is parsed in the configuration of the Import module, the corresponding dependent module is obtained from the dependent module summary file according to the dependent module name, and the dependent modules are uniformly stored in an object modules, When processing the logic script, the dependent module is obtained through the Import module, the definition code of the dependent module is generated according to the fixed format, the definition code of the dependent module is spliced at the beginning of the logic script, the final code string is obtained, and the code string is converted into an executable function using the newFunction() statement, and the executable function is executed to obtain the configuration items of the Vue component. Generate a Vue component instance based on the processing results of the template and the configuration items obtained from the script processing, and render the Vue component instance; Then process the style configuration data into native CSS code, add the CSS selector of the outermost div component of template before each CSS statement in the native CSS code to limit the scope of the CSS statement, create a style tag to store the native CSS code, insert the style tag into the DOM, and complete the processing of the CSS content in the Vue component instance.
2. According to claim 1, a method for customizing and dynamically rendering a Vue component is characterized by: In step 2, execute the executable function to obtain the configuration items of the Vue component, including: The executable function is denoted as fn, and fn is executed through the fn.apply(modules) statement of the apply method, and the execution context is specified as the object modules to obtain the configuration items of the vue component.
3. A method for customizing and dynamically rendering a Vue component according to claim 1, characterized in that In step 2, the Vue framework is assembled in a fixed format to generate the configuration object required for the Vue component instance, and the content of the configuration object is rendered using the Vue built-in component component to achieve the rendering of the Vue component instance.
4. A device for customizing and dynamically rendering a Vue component, characterized in that Including configuration management module and processing module, The configuration management module customizes the three components of the Vue component according to user needs to obtain configuration data. The three components are the component's template structure template, the component's JavaScript behavior script, and the component's style style. The processing module processes template, script and style according to the configuration data to form a custom Vue component: Process the configuration data of the template, including: wrapping the div component on the outermost side of the template configuration content, and setting a unique identifier for the outermost div component for selection by the CSS selector. Processing script configuration data, including: processing the Import module and logic script involved in the script in turn, wherein the dependent module name is parsed in the configuration of the Import module, the corresponding dependent module is obtained from the dependent module summary file according to the dependent module name, and the dependent modules are uniformly stored in an object modules, When processing the logic script, the dependent module is obtained through the Import module, the definition code of the dependent module is generated according to the fixed format, the definition code of the dependent module is spliced at the beginning of the logic script, the final code string is obtained, and the code string is converted into an executable function using the newFunction() statement, and the executable function is executed to obtain the configuration items of the Vue component. Generate a Vue component instance based on the processing results of the template and the configuration items obtained from the script processing, and render the Vue component instance; Then process the style configuration data into native CSS code, add the CSS selector of the outermost div component of template before each CSS statement in the native CSS code to limit the scope of the CSS statement, create a style tag to store the native CSS code, insert the style tag into the DOM, and complete the processing of the CSS content in the Vue component instance.
5. A device for customizing and dynamically rendering a Vue component according to claim 4, characterized in that The processing module executes the executable function to obtain the configuration items of the Vue component, including: The executable function is denoted as fn, and fn is executed through the fn.apply(modules) statement of the apply method. The execution context is specified as the object modules to obtain the configuration items of the vue component.
6. A device for customizing and dynamically rendering a Vue component according to claim 4, characterized in that The processing module assembles the Vue framework according to a fixed format to generate the configuration object required for the Vue component instance, and uses the Vue built-in component component to render the content of the configuration object to achieve the rendering of the Vue component instance.
Citation Information
Patent Citations
Visual page rendering method and device, equipment and storage medium
CN113158101A
VUE-based page generation method, platform, equipment and medium
CN114510233A
Dynamic portal design method and device
CN114968220A
Method and system for realizing rendering of different styles and styles based on set of vue codes
CN116627408A
Visual configuration rendering method and system based on vue componentization, and terminal
WO2022141724A1
