A method for synchronizing Vue.js component data between cross-domain pages
By writing methods to send and receive events between cross-domain pages and using hooks and components for data synchronization, the problem of data transfer between Vue.js components across cross-domain pages is solved, achieving automated data synchronization and seamless communication.
Patent Information
- Application Number
- CN202311729762.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-15
- Publication Date
- 2026-11-20
- Estimated Expiration
- 2043-12-15
AI Technical Summary
Vue.js components cannot synchronize data between cross-domain pages. Due to browser cross-domain security policies, they cannot directly access JavaScript objects embedded in iframe pages.
Cross-domain data transfer is achieved by writing event sending and receiving methods between parent and child web pages, using hooks and components for data synchronization, including serialization and deserialization processing.
It circumvents cross-domain security policy restrictions, enables automatic synchronization of data between parent and child web page components, requires no additional logic maintenance, and is easy to extend.
Smart Images

Figure CN117892025B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, specifically providing a method for synchronizing Vue.js component data across cross-domain pages. Background Technology
[0002] Vue.js is a popular JavaScript framework for building interactive web applications. Features such as reactive data binding, a virtual DOM, component-based development, and directives enable developers to build complex web applications more efficiently. As a component-based framework, Vue.js allows the entire application to be broken down into multiple smaller components, which are then used to build different parts of the application. Each component manages its own state and template and can be reused across different pages.
[0003] Components can pass data to each other via props. In the parent component, data can be passed to child components using `v-bind` or the shorthand colon syntax. Child components can define properties in their own props object to receive this data. If two-way data transfer is required, such as... Figure 1 As shown, the `v-model` directive can be used to bind data to child components in the parent component, enabling automatic listening for update events triggered by the child component and achieving two-way data updates. In the child component, data is received from the parent component via props, and the `$emit` method is used to trigger the input event to update the parent component's data. Thus, when content is entered in the child component, the parent component's message will also be updated accordingly, and changes in the parent component's data will be reflected in the child component.
[0004] When you need to embed one webpage within another, you can use the `iframe` tag to achieve nesting. Using iframes allows you to implement features such as displaying maps, referencing content from other webpages, and embedding videos. It's important to note that if the webpages are from the same origin, the parent page's JavaScript can access the resources of the iframe-embedded page, including JavaScript objects. In this case, achieving the above requirements is relatively simple; you just need to expose methods on the child page that can manipulate the internal logic. However, in cases involving different domains, cross-domain access to the JavaScript objects of the iframe-embedded page is not possible; the two webpages are isolated from each other.
[0005] like Figure 2As shown, in some cases, it may be desirable for iframe-nested web pages to also allow data transfer or even synchronization between components within the parent and child web pages. However, Vue.js is a JavaScript framework whose runtime environment is a single web page. All components based on Vue.js are simply logic encapsulations within a single page. Child web pages nested using iframes and the parent page are two completely independent environments, making it impossible for Vue.js to achieve component data transfer across pages.
[0006] For example, given multiple existing and operational business systems, the goal is to develop a charting tool that provides uniformly styled visual charts, such as bar charts and line charts, along with basic analytical functions. These charts are embedded into each business system via iframes. The business systems transmit data, and the embedded chart page receives the data and displays it as a chart, simplifying the development of data analysis functions for each business system. Since all business systems are developed using the Vue.js technology stack, a method is needed to achieve automatic component data binding between pages. This means that parameters should be passed from within the business system to components within the iframe using traditional component parameter passing methods, and even two-way data binding and cross-page event sending should be implemented when necessary. However, each business system has independent and different access domains, and the charting tool page will also use a separate domain. Therefore, achieving the above objectives requires solving the cross-domain communication problem. Cross-domain communication is due to browser security policies. To protect user data security, browsers prohibit direct access from a page under one domain to resources under another domain.
[0007] Accordingly, there is a need in this field for a new solution to synchronize Vue.js component data across cross-domain pages to address the aforementioned issues. Summary of the Invention
[0008] To overcome the above-mentioned defects, this invention is proposed, which solves the technical problems encountered in the prior art when realizing cross-domain page information synchronization.
[0009] This invention provides a method for synchronizing Vue.js component data between cross-domain pages, applicable to parent and child web pages, wherein the parent web page is an independent page, and the child web page is a page embedded in the parent web page via an iframe. The method includes the following steps:
[0010] Write methods for sending events and corresponding methods for receiving events;
[0011] Based on the sending event method and receiving event method, a first hook and a second hook are written. The first hook is used to implement the logic of sending and receiving data in the parent webpage, and the second hook is used to implement the logic of receiving and sending data in the child webpage.
[0012] A first component is written based on the first hook, and a second component is written based on the second hook. The first component is used to synchronize data between the parent webpage and the first hook, and the second component is used to synchronize data between the child webpage and the second hook.
[0013] Furthermore, before writing the send event method and the corresponding receive event method, the steps also include writing the serialization method and the deserialization method.
[0014] Furthermore, the event sending method includes the following steps:
[0015] Generate a random ID for this event;
[0016] The serialization method is called to serialize the data;
[0017] Assemble the data structure to be sent;
[0018] Determine whether it is necessary to wait for the READY event;
[0019] If not, send data once;
[0020] If so, send the data once, and then send the data again after receiving the READY event.
[0021] Furthermore, the event receiving method includes the following steps:
[0022] Define the callback method for the received event;
[0023] Set the event listener method using the callback method;
[0024] Determine whether a READY event needs to be emitted;
[0025] If not, the event listener cancellation method will be returned directly to the outside world.
[0026] If so, after issuing the READY event, return a method to cancel the event listener.
[0027] Furthermore, the definition of the callback method for receiving an event includes the following steps:
[0028] After entering the callback, retrieve the event data;
[0029] Check if the message name, message frame, and message source match; if not, end the process.
[0030] If yes, then check if id information exists; if not, end the process.
[0031] If so, the deserialization method is invoked to deserialize the payload;
[0032] The process ends after executing the passed-in handler.
[0033] Furthermore, the step of encapsulating the event sending method into a first hook includes the following steps:
[0034] Once the parent webpage has finished loading, there is no need to wait for the READY event; instead, an event carrying initial value data can be sent to the child webpage using the event sending method described above.
[0035] Determine whether it is necessary to wait for the READY event of the child page. If so, start waiting for the READY event and send the event carrying the initial data again when the READY event is received.
[0036] When the initial value data state changes, the event sending method is invoked to send an event carrying the new data to the child webpage;
[0037] Set a callback for receiving events from child web pages, compare the data state of the child web page events with that of the parent web page, and update the parent web page if the data states are inconsistent.
[0038] Furthermore, the step of encapsulating the event receiving method into a second hook includes the following steps:
[0039] Once the child page is ready, a READY event is sent to the parent page.
[0040] The event receiving method initiates a callback upon receiving an event carrying initial value data or an event carrying new data.
[0041] Compare the event carrying the new data with the data state of the child webpage; if the data states are inconsistent, update the child webpage.
[0042] When the data state of a child webpage changes, an event carrying the new data is sent to the parent webpage.
[0043] The beneficial effects of this invention are as follows:
[0044] 1. This invention circumvents browser cross-domain security policies, preventing direct access between parent and child web pages in cross-domain situations. It selectively receives events on the child web page via event listeners, encapsulating the message sending method between parent and child web pages based on this principle. When sending a message, a custom event name and a range of receiving page domains (default is all) can be specified. When setting the listener for receiving messages, the event name, source frame, and domain of the event source can be specified. These options allow for setting target sending and specifying target receiving during event broadcasting.
[0045] 2. Implementing a waiting mechanism. This invention implements a mechanism where the parent webpage postpones the sending of events until the child webpage has finished loading and sent a READY signal before sending the message, thus avoiding message loss.
[0046] 3. This invention is ultimately exposed in the form of components. When using it, no additional logic needs to be written. You only need to use v-model:data on the parent webpage and child webpage respectively to perform two-way data binding on their respective components to complete the usage steps. Afterwards, there is no need to worry about when to notify the other party to update the data, as everything is completed automatically. Attached Figure Description
[0047] The disclosure of this invention will become more readily understood with reference to the accompanying drawings. It will be readily understood by those skilled in the art that these drawings are for illustrative purposes only and are not intended to limit the scope of protection of this invention. Furthermore, similar numbers in the drawings are used to denote similar components, wherein:
[0048] Figure 1 This is a schematic diagram of data transmission in the parent-child relationship component in the background technology of this invention;
[0049] Figure 2 This is a schematic diagram illustrating the inability of Vue.js to pass component data across pages in the background technology of this invention;
[0050] Figure 3 This is a schematic diagram illustrating the working principle of a method for synchronizing Vue.js component data across cross-domain pages according to the present invention;
[0051] Figure 4 This is a schematic diagram illustrating the architectural principle of a method for synchronizing Vue.js component data across cross-domain pages according to the present invention.
[0052] Figure 5 This is a flowchart of the main steps of the event sending method in this invention;
[0053] Figure 6 This is a flowchart of the main steps of the event receiving method in this invention;
[0054] Figure 7 This is a flowchart of the main steps of the callback method in this invention;
[0055] Figure 8 This is a flowchart illustrating the main steps of the interaction between useFrameProvider and useFrameInject in this invention;
[0056] Figure 9 This is a flowchart of the main steps of the IframeProvider component in this invention;
[0057] Figure 10 This is a flowchart of the main steps of the IframeInject component in this invention;
[0058] Figure 11 This is a schematic diagram of a webpage after the actual application of the present invention. Detailed Implementation
[0059] Some embodiments of the present invention will now be described with reference to the accompanying drawings. Those skilled in the art should understand that these embodiments are merely illustrative of the technical principles of the present invention and are not intended to limit the scope of protection of the present invention.
[0060] Figure 3 This is a schematic diagram illustrating the working principle of a method for synchronizing Vue.js component data across cross-domain pages according to the present invention. Figure 3 As shown, the present invention aims to design an encapsulation of cross-page communication logic, and further encapsulate components on top of this logic. When in use, the parent webpage passes parameters to the child component in a normal manner, and the child webpage receives the values in a conventional parameter manner. The component calls the cross-page communication logic to send and receive events, and updates the bound parameter values at appropriate times.
[0061] Figure 4 This is a schematic diagram illustrating the architectural principle of a method for synchronizing Vue.js component data across cross-domain pages according to the present invention. Figure 4 As shown, this invention is designed with a three-layer structure: communication, state, and components. Each layer independently manages a function. The communication structure is implemented by "writing a method to send an event and a corresponding method to receive an event." The state structure is implemented by "writing a first hook and a second hook based on the sending and receiving event methods." The component structure is implemented by "writing a first component based on the first hook and a second component based on the second hook." One method for synchronizing Vue.js component data across cross-domain pages in this embodiment mainly includes the following three steps.
[0062] 1. Write the event sending method and the corresponding event receiving method.
[0063] Because it is necessary to bypass cross-domain security policies, the window.postMessage method is used to pass information between pages. Communication between the two pages is achieved through event broadcasting and selective reception.
[0064] The core code for this step is mainly divided into two parts: ① the send method, which is used to send messages; and ② the on method, which is used to set the action to be performed after a specific message is received.
[0065] Both the parent and child web pages have the function of sending and receiving information. It can be seen that the on method will also be called in the first hook if certain conditions are met; in the second hook, the send method will also be used to send data under certain circumstances.
[0066] The send method performs a specific action, namely, "I want to send data";
[0067] The `on` method registers a callback function, which defines "what I should do when I receive a certain event".
[0068] Therefore, to implement the entire data synchronization logic, both the parent and child web pages will call send and on.
[0069] On the parent webpage, the underlying layer uses `send` to send information to the child webpage, notifying the child webpage that there has been a data change. If it is necessary to wait for the child webpage to be ready, a callback will be registered in the first hook using the `on` method. When the child webpage is received, the callback content is to send information to the child webpage again.
[0070] On the child webpage, the underlying layer uses the on method to receive information sent by the parent webpage. As needed, the second hook actively calls the send method to send an event carrying a ready flag to the parent webpage, notifying the parent webpage that it is ready to receive.
[0071] 1.1 send method
[0072] Figure 5 This is a flowchart of the main steps of the event sending method in this invention. (For example...) Figure 5 As shown, the method for sending an event includes the following steps:
[0073] 1.1.1 Generate a random ID for this event.
[0074] In this embodiment, the random ID refers to a random string with no special meaning. Generating a random ID serves to assign a unique value to the information sent by this method call, used for subsequent logical judgments; it can be understood as giving it a unique name. In the code below, refer to the code "const id=Math.random().toString(16).slice(2,10)" for a simple random number operation to generate the ID value. This is for demonstration purposes only; in actual use, a more robust generation algorithm is required to prevent duplicates.
[0075] 1.1.2 Call the serialization method to serialize the data.
[0076] In the code below, refer to the code "payload:serialize(data)".
[0077] 1.1.3 Assemble the data structure to be sent.
[0078] Please refer to the code below for details:
[0079]
[0080] This code.
[0081] 1.1.4 Determine whether it is necessary to wait for the READY event;
[0082] In the code below, please refer to the code "if(waitTargetReady){".
[0083] 1.1.5 If not, send data once;
[0084] In this embodiment, when a child webpage needs to send data to a parent webpage, calling the send method does not require waiting for the READY event.
[0085] In the code below, refer to the code "target.postMessage(postData,origin)".
[0086] 1.1.6 If so, send the data once, and then send the data again when the READY event is received.
[0087] In this embodiment, when the parent webpage needs to send data to the child webpage, calling the send method requires waiting for the READY event.
[0088] Please refer to the code below for details:
[0089]
[0090] This code.
[0091] 1.2on method
[0092] Figure 6 This is a flowchart of the main steps of the event receiving method in this invention. (For example...) Figure 6 As shown, the method for receiving events includes the following steps:
[0093] 1.2.1 Define the callback method for receiving events.
[0094] Figure 7 This is a flowchart illustrating the main steps of the callback method in this invention. For example... Figure 7 As shown, the callback method further includes:
[0095] 1.2.1.1 After entering the callback, obtain the event data.
[0096] In the code below, refer to the code "let messageEventData = messageEvent.data".
[0097] 1.2.1.2 Determine if the message name matches.
[0098] In the code below, please refer to the code "if(!messageEventData.name||messageEventData.name!==name)return".
[0099] 1.2.1.3 Determine if the message frame matches.
[0100] In the code below, please refer to the code "if(source&&messageEvent.source!==source)return".
[0101] 1.2.1.4 Determine if the message source matches.
[0102] In the code below, please refer to the code "if(origin&&origin!=='*'&&messageEvent.origin!==origin)return".
[0103] 1.2.1.5 Determine if ID information exists.
[0104] In the code below, please refer to the code "if(!messageEventData.id)return".
[0105] 1.2.1.6 Call the aforementioned deserialization method to deserialize the payload;
[0106] In this embodiment, the payload is simply a field name in the sent information throughout the entire process. During the sending process, the data is serialized into a string, and this string is stored in the payload field of the actual sent data. When receiving data, the string value corresponding to the payload field in the received original object is extracted and then deserialized to obtain the data that was intended to be transmitted during the sending process.
[0107] In the code below, refer to the code "const payload = deserialize(messageEventData.payload)".
[0108] 1.2.1.7 The process ends after executing the passed-in handler.
[0109] In the code below, refer to the code "handler(payload)".
[0110] 1.2.2 Set the event listener method through the callback method. The callback method defined above is executed upon receiving an event carrying initial value data.
[0111] Please refer to the code below for details:
[0112] The code is “window.addEventListener('message',windowMessageHandler)”.
[0113] 1.2.3 Determine whether a READY event needs to be emitted;
[0114] In the code below, please refer to the code "if(emitReadyEvent){".
[0115] 1.2.4 If not, the event listener cancellation method will be returned directly to the outside world.
[0116] Please refer to the code below for details:
[0117] return {
[0118] off,
[0119] The code is "}".
[0120] 1.2.5 If so, after issuing the READY event, return the cancellation event listener method to the outside world.
[0121] Please refer to the code below for details:
[0122]
[0123] This code.
[0124] Furthermore, in practical use, to further enhance security, operations such as replacing sensitive information and encryption / decryption are added during serialization and deserialization. Therefore, the core code in this embodiment also includes writing serialization and deserialization methods. Serialization refers to the process of converting a JavaScript object or value into a string, and deserialization refers to the process of converting a string back into a JavaScript object or value. This embodiment uses the built-in JSON serialization and deserialization methods of the JavaScript language to replace this step. If only cross-domain page data synchronization is implemented, this step is unnecessary because it only converts the data into a string before sending and converts the string back into object format upon receiving.
[0125] The core code for the first step in this embodiment is as follows:
[0126]
[0127]
[0128]
[0129] In the code above, the serialization method (named serialize) and the deserialization method (named deserialize) are public functions declared in the file header. The sending method (named send) and the receiving method (named on) in the code below call the serialization method to process the data to be sent before sending, and call the deserialization method that matches the serialization method after receiving the data to parse the received data into a format that the program can process.
[0130] 2. Based on the sending event method and the receiving event method, write a first hook and a second hook. The first hook is used to implement the logic of sending and receiving data on the parent webpage, and the second hook is used to implement the logic of receiving and sending data on the child webpage.
[0131] The first hook is named `useFrameProvider`, and the second hook is named `useFrameInject`. The main functions of these two hooks are: ① to maintain an internal data state, broadcasting changes when the state changes, and updating the internal data upon receiving an update signal; ② to maintain consistency between the internal data state and the specified data in the component that calls this hook.
[0132] Figure 8 This is a flowchart illustrating the main steps of the interaction between useFrameProvider and useFrameInject in this invention. Figure 8 As shown, the interaction steps include:
[0133] 2.1 Once the parent webpage has finished loading, there is no need to wait for the READY event. The useFrameProvider sends an event carrying initial value data to the child webpage through the event sending method.
[0134] Please refer to the code below for details:
[0135]
[0136] and
[0137]
[0138]
[0139] These two code snippets.
[0140] 2.2 Once the child page is ready, useFrameInject sends a READY event to the parent page.
[0141] In the code below, refer to the code "emitReadyEvent:true," for a specific example.
[0142] 2.3 Start waiting for the READY event, and when the READY event is received, useFrameProvider sends the event again with the initial value data;
[0143] In the code below, refer to the code "waitTargetReady:true," for a specific example.
[0144] 2.4 useFrameInject initiates a callback after receiving an event carrying initial value data or an event carrying new data through the event receiving method described above;
[0145] Please refer to the code below for details:
[0146]
[0147] This code.
[0148] 2.5 When the initial value data state changes, useFrameProvider calls the send event method to send an event carrying the new data to the child webpage;
[0149] Please refer to the code below for details:
[0150]
[0151] This code.
[0152] 2.6 useFrameInject compares the new data with the data state of the child webpage. If the data state is inconsistent, the child webpage is updated.
[0153] Please refer to the code below for details:
[0154]
[0155] This code.
[0156] 2.7 useFrameProvider sets the callback for receiving events from child web pages. It compares the data status of the child web page events with that of the parent web page. If the data status is inconsistent, it updates the parent web page.
[0157] Please refer to the code below for details:
[0158]
[0159] This code.
[0160] 2.8 When the data state of a child webpage changes, useFrameInject sends an event to the parent webpage that includes the child webpage.
[0161] Please refer to the code below for details:
[0162]
[0163] This code.
[0164] The core code for the second step in this embodiment is as follows:
[0165]
[0166]
[0167]
[0168] 3. Write a first component based on the first hook and a second component based on the second hook. The first component is used to synchronize data between the parent webpage and the first hook, and the second component is used to synchronize data between the child webpage and the second hook.
[0169] The first component is named IframeProvider, and the second component is named IframeInject. The IframeProvider component is used in the parent webpage, using v-model:data to bind a data source two-way, automatically keeping this data source synchronized with the data source of the child webpage; the IframeInject component is used in the child webpage, using v-model:data to bind a data source two-way, automatically keeping this data source synchronized with the data source of the parent webpage.
[0170] In this embodiment, the function of the component is to wrap the code that uses hooks and connect the page and the hook to avoid duplication; otherwise, this code would have to be rewritten every time the hook is used.
[0171] 3.1 IframeProvider Component
[0172] Figure 9 This is a flowchart illustrating the main steps of the IframeProvider component in this invention. Figure 9 As shown, the IframeProvider component is mainly responsible for synchronizing data between the parent webpage and useFrameProvider, including:
[0173] 3.1.1 Passing parent webpage data to useFrameProvider
[0174] The data parameter is passed from the parent webpage to the IframeProvider component. In the code below, refer to the code "data:{type:Object,default:()=>({})}," for details.
[0175] The `data` parameter is passed from the `IframeProvider` component to `useFrameProvider`. See the code below for details:
[0176]
[0177] This is the code.
[0178] 3.1.2 Update the parent webpage with useFrameProvider data
[0179] The data parameter is updated from useFrameProvider to the IframeProvider component. See the code below for the specific code snippet: "useFrameProvider(iframeRef,iframeData)".
[0180] To update the data parameter from the IframeProvider component to the parent webpage, refer to the code snippet "emits('update:data',val)" in the following text.
[0181] The core code of the IframeProvider component in this embodiment is as follows:
[0182]
[0183]
[0184] 3.2 IframeInject Component
[0185] Figure 10 This is a flowchart illustrating the main steps of the IframeInject component in this invention. Figure 10 As shown, the IframeInject component is mainly responsible for synchronizing data between child web pages and useFrameInject, including:
[0186] 3.2.1 Passing child webpage data to useFrameInject
[0187] The data parameter is passed from the child page to the IframeInject component. In the code below, refer to the code "data:{type:Object,default:()=>({})}," for details.
[0188] The `data` parameter is passed from the `IframeInject` component to `useFrameInject`. See the code below for details:
[0189]
[0190] This is the code.
[0191] 3.2.2 Update the data in useFrameInject to child web pages
[0192] Update the data parameter from useFrameInject to the IframeInject component. See the code below for the specific code snippet: "useFrameInject(iframeData)".
[0193] To update the data parameter from the IframeInject component to the child webpage, refer to the code snippet "emits('update:data',val)" in the code below.
[0194] The core code of the IframeProvider component in this embodiment is as follows:
[0195]
[0196]
[0197] It should be noted that although the steps in the above embodiments are described in a specific order, those skilled in the art will understand that in order to achieve the effects of the present invention, different steps do not necessarily have to be executed in such an order. They can be executed simultaneously (in parallel) or in other orders, and these variations are all within the scope of protection of the present invention.
[0198] By implementing the above-described solution, the present invention has the following advantages:
[0199] Advantage 1: There is no need to worry about whether the two pages are from the same origin. This solution is not subject to the same-origin policy access restrictions.
[0200] Advantage 2: Automated data synchronization. Simply bind the data to the FrameProvider and FrameInject components within the parent and child web pages to complete the usage steps. You no longer need to worry about when to notify the other party to update the data; everything is done automatically.
[0201] Advantage 3: Easy to expand. Due to the logical layered design, it is easy to add additional functions, such as encryption when sending data, logging function, sending custom events, event throttling, etc.
[0202] If this solution is not adopted, the following problems may occur:
[0203] 1. If you directly use JavaScript to manipulate the child iframe, or get window.parent in the child iframe, the browser will block access when the page crosses domains, and the solution will fail.
[0204] 2. If, like this solution, an event-based message passing mechanism is chosen, but the layered structure and encapsulation of this solution are not used, the disadvantages are:
[0205] a. Event sending and data management are mixed together, resulting in code coupling and logic coupling, which is not conducive to maintenance and is not easy to extend.
[0206] b. Sending information to the parent page before the child page has finished loading results in data loss.
[0207] c. Instead of using components to provide services, manually maintaining data changes during use would increase the amount of code and complexity compared to this approach.
[0208] The code example after using this invention is as follows
[0209] Parent webpage:
[0210]
[0211] Child webpages:
[0212]
[0213] Figure 11 This is a schematic diagram of a webpage after the actual application of this invention. For example... Figure 11 As shown, this invention can ensure that the data of the parent webpage is always synchronized with the data of the child webpage. When the "Add" button of the parent webpage is clicked, the data.count of the parent webpage is incremented by 1, from 1 to 2, and the data.count of the child webpage is also incremented by 1, from 1 to 2. When the "Add" button of the child webpage is clicked, the data.count of the child webpage is incremented by 1, from 2 to 3, and the data.count of the parent webpage is also incremented by 1, from 2 to 3.
[0214] The technical solution of the present invention has been described above with reference to the preferred embodiments shown in the accompanying drawings. However, it will be readily understood by those skilled in the art that the scope of protection of the present invention is obviously not limited to these specific embodiments. Without departing from the principles of the present invention, those skilled in the art can make equivalent changes or substitutions to the relevant technical features, and the technical solutions after such changes or substitutions will all fall within the scope of protection of the present invention.
Claims
1. A method for synchronizing Vue.js component data between cross-domain pages, applied between a parent webpage and a child webpage, wherein the parent webpage is an independent page, and the child webpage is a page embedded into the parent webpage via an iframe method, characterized in that... Including the following steps: Write methods for sending events and corresponding methods for receiving events; Based on the sending event method and receiving event method, a first hook and a second hook are written. The first hook is used to implement the logic of sending and receiving data in the parent webpage, and the second hook is used to implement the logic of receiving and sending data in the child webpage. A first component is written based on the first hook, and a second component is written based on the second hook. The first component is used to synchronize data between the parent webpage and the first hook, and the second component is used to synchronize data between the child webpage and the second hook. The process of writing the first hook based on the event sending method includes the following steps: Once the parent webpage has finished loading, there is no need to wait for the READY event; instead, an event carrying initial value data can be sent to the child webpage using the event sending method described above. Determine whether it is necessary to wait for the READY event of the child page. If so, start waiting for the READY event and send the event carrying the initial data again when the READY event is received. When the initial value data state changes, the event sending method is invoked to send an event carrying the new data to the child webpage; Set a callback for receiving events from child web pages, compare the data state of the child web page events with that of the parent web page, and update the parent web page if the data states are inconsistent.
2. The method for synchronizing Vue.js component data across cross-domain pages according to claim 1, characterized in that, Before writing the send event method and the corresponding receive event method, the steps also include writing the serialization method and the deserialization method.
3. The method for synchronizing Vue.js component data across cross-domain pages according to claim 2, characterized in that, The method for sending events includes the following steps: Generate a random ID for this event; The serialization method is called to serialize the data; Assemble the data structure to be sent; Determine whether it is necessary to wait for the READY event; If not, send data once; If so, send the data once, and then send the data again after receiving the READY event.
4. The method for synchronizing Vue.js component data across cross-domain pages according to claim 2, characterized in that, The method for receiving events includes the following steps: Define the callback method for the received event; Set the event listener method using the callback method; Determine whether a READY event needs to be emitted; If not, the event listener cancellation method will be returned directly to the outside world. If so, after issuing the READY event, return a method to cancel the event listener.
5. The method for synchronizing Vue.js component data across cross-domain pages according to claim 4, characterized in that, The definition of the callback method for receiving events includes the following steps: After entering the callback, retrieve the event data; Check if the message name, message frame, and message source match; if not, end the process. If yes, then check if id information exists; if not, end the process. If so, the deserialization method is invoked to deserialize the payload; The process ends after executing the passed-in handler.
6. The method for synchronizing Vue.js component data across cross-domain pages according to claim 1, characterized in that, The step of writing a second hook based on the event receiving method includes the following steps: Once the child page is ready, a READY event is sent to the parent page. The event receiving method initiates a callback upon receiving an event carrying initial value data or an event carrying new data. Compare the event carrying the new data with the data state of the child webpage; if the data states are inconsistent, update the child webpage. When the data state of a child webpage changes, an event carrying the new data is sent to the parent webpage.
Citation Information
Patent Citations
Web page cross-domain communication method and device
CN103095762A
Web page cross-domain communication method and device
CN104301379A
