Svelte has been around for a long time, and I have always wanted to write an easy-to-understand principle analysis article. After putting it off for so long, I finally wrote it. Demo1 First, let's look at the compilation process. Consider the following <h1>{count}</h1> <script> let count = 0; </script> After being compiled by the compiler, this code generates the following code, which consists of three parts: Declaration statement of // Omit some code... function create_fragment(ctx) { let h1; return { c() { h1 = element("h1"); h1.textContent = `${count}`; }, m(target, anchor) { insert(target, h1, anchor); }, d(detaching) { if (detaching) detach(h1); } }; } let count = 0; class App extends SvelteComponent { constructor(options) { super(); init(this, options, null, create_fragment, safe_not_equal, {}); } } export default App; create_fragment First, let's look at the h1 = element("h1"); h1.textContent = `${count}`; insert(target, h1, anchor); The function insert(target, node, anchor) { target.insertBefore(node, anchor || null); } if (detaching) detach(h1); The function detach(node) { node.parentNode.removeChild(node); } If you look closely at the flowchart, you will find that the compiled product of This is because It can be found that the SvelteComponent Each component corresponds to a class App extends SvelteComponent { constructor(options) { super(); init(this, options, null, create_fragment, safe_not_equal, {}); } } To summarize, the compilation result of the dotted part in the flowchart in Demo that can change state Now modify <h1 on:click="{update}">{count}</h1> <script> let count = 0; function update() { count++; } </script> The compiled product changes, and the changes of // From the top-level declaration of the module let count = 0; // Become an instance method function instance($$self, $$props, $$invalidate) { let count = 0; function update() { $$invalidate(0, count++, count); } return [count, update]; } // Define 3 Apps in the template <App/> <App/> <App/> // When count is immutable, the page is rendered as: <h1>0</h1> <h1>0</h1> <h1>0</h1> When <h1>0</h1> <h1>3</h1> <h1>1</h1> So each
Once found, the variable will be extracted to At the same time, if the statement that performs the above operation can be referenced through the template, the statement will be wrapped by In
So the statement that changes // update in source code function update() { count++; } // Update in compiled instance function update() { $$invalidate(0, count++, count); }
The c() { h1 = element("h1"); //The value of count is obtained from ctx t = text(/*count*/ ctx[0]); }, m(target, anchor) { insert(target, h1, anchor); append(h1, t); // Event binding dispose = listen(h1, "click", /*update*/ ctx[1]); }, p(ctx, [dirty]) { // set_data will update the text node stored in t if (dirty & /*count*/ 1) set_data(t, /*count*/ ctx[0]); }, d(detaching) { if (detaching) detach(h1); // Event unbinding dispose(); } The In // Reference multiple states in UI<h1 on:click="{count0++}">{count0}</h1> <h1 on:click="{count1++}">{count1}</h1> <h1 on:click="{count2++}">{count2}</h1> The corresponding p(new_ctx, [dirty]) { ctx = new_ctx; if (dirty & /*count*/ 1) set_data(t0, /*count*/ ctx[0]); if (dirty & /*count1*/ 2) set_data(t2, /*count1*/ ctx[1]); if (dirty & /*count2*/ 4) set_data(t4, /*count2*/ ctx[2]); }, The complete update steps
The above is the detailed content of the detailed explanation of the implementation principles of Svelte in JavaScript development. For more information about the implementation principles of Svelte, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: A MySQL migration plan and practical record of pitfalls
>>: HTML+CSS makes div tag add delete icon in the upper right corner sample code
Open DREAMWEAVER and create a new HTML. . Propert...
ffmpeg is a very powerful audio and video process...
Alphabetical DTD: Indicates in which XHTML 1.0 DT...
A few days ago, when I was adjusting a module of a...
To do a paginated query: 1. For MySQL, it is not ...
1. Introduction Sometimes, after the web platform...
Table of contents 1. What is event delegation? 2....
Disable swap If the server is running a database ...
A colleague asked me to help him figure out why m...
Click here to return to the 123WORDPRESS.COM HTML ...
Table of contents Achieve results Available plugi...
Recently, the Vue project needs to refresh the da...
This article example shares the specific code of ...
Table of contents 1. Basic use of css(): 1.1 Get ...
Table of contents 1. React Hooks vs. Pure Functio...