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
Table of contents 1 View the current database con...
Today I will share with you a neon button animati...
I believe everyone has used JD. There is a very c...
When you write buttons (input, button), you will f...
Table of contents Linux-Use MyCat to implement My...
A style sheet describes how a document should be ...
Table of contents 1. Home Page Production 1. Prod...
This article introduces Nginx from compilation an...
1.fullpage.js Download address https://github.com...
Use meta to implement timed refresh or jump of th...
Table of contents Written in front Requirements A...
Table of contents Preface How to use Summarize Pr...
When newbies develop div+css, they need to name t...
The Meta tag is an auxiliary tag in the head area...
Preface It took two days to reconstruct a puzzle ...