We can create jsx/tsx files directly The project structure this time is as follows: Use it like this in the vue file // index.vue <template> <div class="wrapper"> <Common :opt="list" /> </div> </template> <script lang="ts"> import { Component, Vue } from "vue-property-decorator"; import Common from "./components/Common"; @Component({ name: "App", components: Common, }, }) export default class App extends Vue { private list = ["I want to go to Taobao", "I want to go to Baidu", "I want to go to JD"]; } </script> tsx writes like this import { CreateElement } from 'vue'; import { Component, Vue, Prop } from 'vue-property-decorator'; @Component({ name: 'Common' }) export default class Common extends Vue { @Prop(Object) opt!: any[] render(h: CreateElement) { return <span> { this.opt.map((it) => { return <span style="marginRight:10px">{it}</span> }) } </span> } } Take a look at the page Someone may have noticed that I also referenced A brief introduction to parameter passing: First parameter: { The second parameter: The third parameter: { The rendering function brings a lot of flexibility to Vue. In the past, if you wanted to customize the insertion of something in a child component, you had to write a lot of // Transform the data of index.vue above private list = [ { render: () => ["a", { style: { color: "red" } }, "I want to go to Taobao"] }, { render: () => ["a", { style: { color: "green" } }, "I want to go to JD."] }, { render: () => ["a", { style: { color: "pink" } }, "I want to go to Baidu"] }, ]; This is written in tsx: { this.opt.map((it) => { return h(...it.render()) }) } You can render a fancy page. We can also play like this: // tsx transformation <span> { this.opt.map((it) => { return it.render(h) }) } </span> We can do this on the index.vue page: // index.vue private list = [ { render: (h: CreateElement) => h("a", { style: { color: "red", marginRight: "5px" } }, "I want to go to Taobao"), }, { render: (h: CreateElement) => h("a", { style: { color: "green", marginRight: "5px" } }, "I want to go to JD"), }, { render: (h: CreateElement) => h("a", { style: { color: "pink", marginRight: "5px" } }, "I want to go to Baidu"), }, ]; The result is the same fancy We can also render gibberish labels! // index.vue transformation { render: (h: CreateElement) => h( "h1", { style: { color: "green", marginRight: "5px" }, }, "I'm going to JD." ), }, We can define events in the rendering function as we like: // index.vue private list = [ { render: (h: CreateElement) => h( "a", { style: { color: "red", marginRight: "5px" }, on: { click: () => this.iWillGoWhere("TB"), }, }, "I want to go to Taobao" ), }] iWillGoWhere(type: string) { const goWhere: any = { TB: () => { alert("I'm going to Taobao!"); }, JD: () => { alert("I'm going to JD!"); }, BD: () => { alert("I want to go to Baidu!"); }, }; goWhere[type](); } That’s it! This is the end of this article about how to write react in a vue project. For more information about writing react in a vue project, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed analysis of MySQL optimization of like and = performance
>>: How to reset the root password in Linux mysql-5.6
Request logic Front-end --> Request nginx via ...
Method 1 Copy code The code is as follows: documen...
To achieve the association of the frame window, th...
I was bored and sorted out some simple exercises ...
1. Overview 1.1 Basic concepts: Docker is an open...
Table of contents 1. Bootstrap5 breakpoints 1.1 M...
HTML consists of two parts: head and body ** The ...
Mysql join query 1. Basic concepts Connect each r...
Preface A Docker image consists of a Dockerfile a...
Table of contents What is a headless browser? Why...
<br />With the increase of bandwidth, there ...
Table of contents summary Overall process front e...
Table of contents 1. Pull the image 1.1 Pull the ...
In our daily development work, text overflow, tru...
1 Download MySQL8 from the official website and i...