1. UnderstandingDocumentation: https://v3.cn.vuejs.org/guide/render-function.html#dom-%E6%A0%91 What exactly does h() return? Not actually an actual DOM element. A more accurate name for it might be createNodeDescription, because the information it contains tells Vue what kind of node needs to be rendered on the page, including description information for its child nodes. We describe such nodes as "virtual nodes", which are often abbreviated as VNode. "Virtual DOM" is what we call the entire VNode tree built by the Vue component tree. 2. UseDocumentation: https://v3.cn.vuejs.org/guide/render-function.html#h-%E5%8F%82%E6%95%B0 1. h() parametersThe h() function is a utility for creating a VNode. Perhaps it could be more accurately named createVNode(), but due to frequent use and brevity it is referred to as h(). It accepts three parameters: // @returns {VNode} h( // {String | Object | Function} tag // An HTML tag name, a component, an asynchronous component, or // a functional component. // // Required. 'div', // {Object} props // Objects corresponding to attributes, props, and events. // This will be used in the template. // // Optional (During development, it is recommended to pass it. If there is no need to pass it, pass null) {}, // {String | Array | Object} children // Sub VNodes, constructed using `h()`, // Or use a string to get a "text VNode" or // an object with slots. // // Optional. [ 'Some text comes first.', h('h1', 'A headline'), h(MyComponent, { someProp: 'foobar' }) ] ) 2. Simple to use3. Implement a counter case<script> /* To set the style, you need to import it here. If you use the style tag, you cannot write scoped, which is not conducive to setting local styles, so it is not recommended*/ import "./style.css" import { h, ref } from "vue"; export default { // How to write data // data() { // return { // counter: 0 // } // }, setup() { const counter = ref(0) return { counter } }, /** * When using setup, you can also use this below. The import of render has this bound, so the following value should use this */ render() { return h("div", null, [ h("h1", null, `Current count: ${this.counter}`), h("button", { onClick: () => this.counter++, class: "button" }, "add 1"), h("button", { onClick: () => this.counter--, class: "button" }, "minus 1") ]) } } </script> Modify to pure setup writing: <script> /* To set the style, you need to import it here. If you use the style tag, you cannot write scoped, which is not conducive to setting local styles, so it is not recommended*/ import "./style.css" import { h, ref } from "vue"; export default { // How to write data // data() { // return { // counter: 0 // } // }, setup() { const counter = ref(0) return () => { return h("div", null, [ h("h1", null, `Current count: ${counter.value}`), h("button", { onClick: () => counter.value++, class: "button" }, "add 1"), h("button", { onClick: () => counter.value--, class: "button" }, "minus 1") ]) } } } </script> 4. Use of function components and slots1) Parent component <script> import { h, ref } from "vue"; import Test from "./components/Test.vue" export default { setup() { return {} }, render() { return h(Test, null, { // default corresponds to a function, default is the default slot default: props => h("span", null, "Content passed to the component by the parent:" + props.name) }) } } </script> 2) Subcomponents <script> import { h } from "vue"; export default { /** * Receive the content passed in by the husband */ render() { return h("div", null, [ h("div", null, "I am a child component"), /** * Here we can judge whether others have passed in * You can also write null to display nothing * You can also pass in a parameter, using the function to pass the parameter */ this.$slots.default ? this.$slots.default({ name: "哈哈哈哈" }) : h("div", null, "I am the default value of the child component") ]) } } </script> Note: In the project, if you write code like above, it will be too painful, so you have to use JSX at this time. 3. Use of JSX1. Understanding of JSXWe usually convert jsx through Babel (jsx written in React is converted through babel); For Vue, we only need to configure the corresponding plug-in in Babel; Documentation: https://v3.cn.vuejs.org/guide/render-function.html#jsx 2. Download the Babel plugin to support Vue (now it seems that the scaffolding directly supports it)npm install @vue/babel-plugin-jsx -D 3. Configure babel1) Create .babel.config.js in the root directory 2) Add the following code to .babel.config.js module.exports = { presets: [ "@/vue/cli-plugin-babel/preset" ], plugins: [ "@vue/babel-plugin-jsx" ] } 4. Easy to use<script> import { ref } from 'vue' export default { setup() { let counter = ref(0) return { counter } }, render() { return ( <div> <div>Use of JSX</div> <h2>Current number: {this.counter}</h2> </div> ) } } </script> 5. Counter Case<script> import { ref } from '@vue/reactivity' export default { setup() { let counter = ref(0) function add() { counter.value++ } function decrement() { counter.value-- } return { counter, add, decrement } }, render() { return ( <div> <div>Use of JSX</div> <h2>Current number: {this.counter}</h2> <button onClick={this.add}>Add 1</button> <button onClick={this.decrement} >Decrement 1</button> </div> ) } } </script> 6. Use of components and slots1) Parent component <script> import { ref } from '@vue/reactivity' import Test from "./components/Test.vue" export default { setup() { let counter = ref(0) function add() { counter.value++ } function decrement() { counter.value-- } return { counter, add, decrement } }, render() { return ( <div> <div>Use of JSX</div> {/* If this block uses variables or methods in setup, add this */} <h2>Current number: {this.counter}</h2> <button onClick={this.add}>Add 1</button> <button onClick={this.decrement} >Decrement 1</button> <hr /> <Test> {/* Write the passed content here, that is, pass it into a slot*/} {{ default: props => <p>I am passed from parent to child</p> }} </Test> </div> ) } } </script> 2) Subcomponents <script> export default { /** * Receive the content passed in by the husband */ render() { return ( <div> <p>I am a component</p> {/* This piece may not be worn, and you need to display a default value. In this case, you need to use the ternary operator*/} {this.$slots.default()} </div> ) } } </script> Note: If you want to use h function to write components, please read the documentation carefully. The above explanation is just for beginners. This is the end of this article on the detailed use of Vue h function. For more relevant Vue h function 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:
|
<<: Complete MySQL Learning Notes
>>: Solve the problem of HTML automatic formatting after saving in vscode
Solution Add position:relative to the parent elem...
Detailed example of clearing tablespace fragmenta...
Table of contents Make scrolling smoother BetterS...
Author: Guan Changlong is a DBA in the Delivery S...
Introduction: MySQL database recovery by time poi...
one, G:\MySQL\MySQL Server 5.7\bin> mysqld --i...
Install mysql5.7.18 on CentOS6.7 1. Unzip to the ...
Mysql stored procedure 1. Create stored procedure...
Table of contents 1. Decoupled assignment of arra...
Declaring variables Setting Global Variables set ...
Prerequisites A cloud server (centOS of Alibaba C...
Preface I believe everyone knows that indexes are...
I saw a good idea and recorded it. I have used jQ...
What is the reason for the Last_IO_Errno:1236 err...
When starting MongoDB, the prompt is: error while...