I believe that dynamic components will be used most of the time in the development process. When we need to switch states between different components, dynamic components can meet our needs very well. The core of this is the use of the component tag and the is attribute. Basic description// vue <div id="app"> <button @click="changeTabs('child1')">child1</button> <button @click="changeTabs('child2')">child2</button> <button @click="changeTabs('child3')">child3</button> <component :is="chooseTabs"> </component> </div> // js var child1 = { template: '<div>content1</div>', } var child2 = { template: '<div>content2</div>' } var child3 = { template: '<div>content3</div>' } var vm = new Vue({ el: '#app', components: child1, child2, child3 }, methods: { changeTabs(tab) { this.chooseTabs = tab; } } }) AST parsingThe interpretation of <component> is consistent with the previous articles. It will start from the AST parsing stage. The process will not focus on every detail, but will specifically explain the differences from previous processing methods. The differences in dynamic component parsing are focused on processComponent. Due to the existence of the is attribute on the tag, it will mark the component attribute on the final ast tree. // Analysis of dynamic components function processComponent (el) { var binding; // Get the value corresponding to the is attribute if ((binding = getBindingAttr(el, 'is'))) { // The ast tree has an additional component attribute el.component = binding; } if (getAndRemoveAttr(el, 'inline-template') != null) { el.inlineTemplate = true; } } Render FunctionWith the ast tree, the next step is to generate an executable render function based on the ast tree. Due to the component attribute, the generation process of the render function will go through the genComponent branch. //render function generation function var code = generate(ast, options); // implementation of generate function function generate (ast,options) { var state = new CodegenState(options); var code = ast ? genElement(ast, state) : '_c("div")'; return { render: ("with(this){return " + code + "}"), staticRenderFns: state.staticRenderFns } } function genElement(el, state) { ··· var code; // Dynamic component branch if (el.component) { code = genComponent(el.component, el, state); } } The processing logic for dynamic components is actually very simple. When there is no inline template flag (which will be discussed later), the subsequent child nodes are obtained for splicing. The only difference from ordinary components is that the first parameter of _c is no longer a specified string, but a variable representing the component. // Processing function for dynamic components genComponent ( componentName, el, state ) { // When the inlineTemplate attribute is set, children is null var children = el.inlineTemplate ? null : genChildren(el, state, true); return ("_c(" + componentName + "," + (genData$2(el, state)) + (children ? ("," + children) : '') + ")") } Comparison between normal components and dynamic componentsRender function of ordinary components "with(this){return _c('div',{attrs:{"id":"app"}},[_c('child1',[_v(_s(test))])],1)}" Dynamic component render function "with(this){return _c('div',{attrs:{"id":"app"}},[_c(chooseTabs,{tag:"component"})],1)}" To summarize briefly, the difference between dynamic components and ordinary components is:
Dynamic components in factory function formYou can also use dynamic components in the form of factory functions as follows: const AsyncComponent = () => ({ // Component to load (should be a `Promise` object) component: import('./MyComponent.vue'), //Component loading used when asynchronous component loading: LoadingComponent, // Component used when loading fails error: ErrorComponent, // Display the delay time of the component during loading. The default value is 200 (milliseconds) delay: 200, // If a timeout is provided and the component load times out, // Use the component used when loading fails. Default value is: `Infinity` timeout: 3000 }); components: AsyncComponent, }, SummarizeThis is the end of this article about advanced usage of Vue dynamic components. For more relevant Vue dynamic component content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Solve the problem of managing containers with Docker Compose
>>: HTML input box optimization to improve user experience and ease of use
Preface: The Linux host is relatively easy to han...
Mac node delete and reinstall delete node -v sudo...
Introduction to HTML page source code layout This...
My mysql version is MYSQL V5.7.9, please use the ...
Table of contents 1. Generate AST abstract syntax...
Table of contents 1. Reverse the numbers 2. Get t...
I used the dialog in closure and drew a dialog wit...
Table of contents 1. Overview 2. Routing Navigati...
Table of contents Implementation ideas: Step 1: C...
1. Add in package.json "main": "el...
pssh is an open source software implemented in Py...
This article shares the specific code of the jQue...
Cluster Deployment Overview 172.22.12.20 172.22.1...
Recently, I have been studying the MySQL database...
After resetting the system, the MySQL database th...