自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

教練,怎么在vue項(xiàng)目里寫(xiě)react?

開(kāi)發(fā) 前端
我承認(rèn)了我是標(biāo)題黨,本篇文章是在vue項(xiàng)目里寫(xiě)tsx的一篇介紹。作為一個(gè)reacter,目前的業(yè)務(wù)天天使用vue2+ts讓我十分的不舒服。

 [[424253]]

1.前言

我承認(rèn)了我是標(biāo)題黨,本篇文章是在vue項(xiàng)目里寫(xiě)tsx的一篇介紹。作為一個(gè)reacter,目前的業(yè)務(wù)天天使用vue2+ts讓我十分的不舒服。我對(duì)于vue也不是很熟悉,想回到我的react時(shí)代。于是在查詢(xún)官網(wǎng)之后發(fā)現(xiàn)在vue里面寫(xiě)jsx也挺有意思的,遂記錄。

2.正文

vue2+ts的項(xiàng)目配置這里就不展開(kāi)了,網(wǎng)上一搜一大推。

index.vue是頁(yè)面路由,存放各個(gè)組件和公用邏輯。components文件夾中存放我的tsx組件。

接下來(lái)就開(kāi)始寫(xiě)tsx。

你可以直接創(chuàng)建jsx/tsx文件

這次的項(xiàng)目結(jié)構(gòu)是這樣的:

在vue文件里這么使用

  1. // index.vue 
  2. <template> 
  3.   <div class="wrapper"
  4.     <Common :opt="list" /> 
  5.   </div> 
  6. </template> 
  7.   
  8. <script lang="ts"
  9. import { Component, Vue } from "vue-property-decorator"
  10. import Common from "./components/Common"
  11.  
  12. @Component({ 
  13.   name: "App"
  14.   components: { 
  15.     Common, 
  16.   }, 
  17. }) 
  18. export default class App extends Vue { 
  19.   private list = ["我要去淘寶""我要去百度""我要去京東"]; 
  20. </script> 

tsx這么寫(xiě)

  1. import { CreateElement } from 'vue'
  2. import { Component, Vue, Prop } from 'vue-property-decorator'
  3.  
  4. @Component({ 
  5.     name: 'Common' 
  6. }) 
  7. export default class Common extends Vue { 
  8.     @Prop(Object) opt!: any[] 
  9.  
  10.     render(h: CreateElement) { 
  11.         return <span> 
  12.             { 
  13.                 this.opt.map((it) => { 
  14.                     return <span style="marginRight:10px">{it}</span> 
  15.                 }) 
  16.             } 
  17.         </span> 
  18.     } 

在來(lái)看一下頁(yè)面

這該死的react既視感,竟是如此的誘人

可能有心者注意到了 我還引用了一個(gè) CreateElement ,這是干嘛的呢。這玩意叫 渲染函數(shù) 。不喜歡讀vue那么大串的文檔的兄弟看這里。簡(jiǎn)單解釋?zhuān)哼@個(gè)東西可以渲染一個(gè)vnode節(jié)點(diǎn)。 它比模板更接近編譯器。 什么意思呢?意思就是模板語(yǔ)法也會(huì)編譯成渲染函數(shù)。所以我們直接用渲染函數(shù)不就相當(dāng)于節(jié)省了模板語(yǔ)法到渲染函數(shù)的過(guò)程。四舍五入項(xiàng)目性能又是一個(gè)大的提升!

簡(jiǎn)單介紹一下傳參:

第一個(gè)參數(shù) : {String | Object | Function} 一個(gè) HTML 標(biāo)簽名、組件選項(xiàng)對(duì)象,或者 resolve 了上述任何一種的一個(gè) async 函數(shù)。必填項(xiàng)。

第二個(gè)參數(shù) : Object 一個(gè)與模板中 attribute 對(duì)應(yīng)的數(shù)據(jù)對(duì)象。

第三個(gè)參數(shù) : {String | Array} 文本節(jié)點(diǎn)或子級(jí)虛擬節(jié)點(diǎn) (VNodes)。

渲染函數(shù)給vue帶來(lái)了很多的靈活性,以前你想自定義在子組件里插入東西,得寫(xiě)一大堆的插槽。 <slot> 。有了渲染函數(shù)我們可以這么玩。

  1. // 改造一下上面的index.vue的data 
  2.  
  3.   private list = [ 
  4.     { render: () => ["a", { style: { color: "red" } }, "我要去淘寶"] }, 
  5.     { render: () => ["a", { style: { color: "green" } }, "我要去京東"] }, 
  6.     { render: () => ["a", { style: { color: "pink" } }, "我要去百度"] }, 
  7.   ]; 

tsx中這么寫(xiě):

  1.                 this.opt.map((it) => { 
  2.                     return h(...it.render()) 
  3.                 }) 
  4.             } 

就可以渲染出花里胡哨的頁(yè)面了

我們還可以這么玩:

  1. // tsx改造 
  2. <span> 
  3.             { 
  4.                 this.opt.map((it) => { 
  5.                     return it.render(h) 
  6.                 }) 
  7.             } 
  8. </span> 
  9.  
  10.  
  11. 在index.vue頁(yè)面我們就可以這么玩: 
  12. // index.vue 
  13. private list = [ 
  14.     { 
  15.       render: (h: CreateElement) => 
  16.         h("a", { style: { color: "red", marginRight: "5px" } }, "我要去淘寶"), 
  17.     }, 
  18.     { 
  19.       render: (h: CreateElement) => 
  20.         h("a", { style: { color: "green", marginRight: "5px" } }, "我要去京東"), 
  21.     }, 
  22.     { 
  23.       render: (h: CreateElement) => 
  24.         h("a", { style: { color: "pink", marginRight: "5px" } }, "我要去百度"), 
  25.     }, 
  26.   ]; 

結(jié)果也是同樣的花哨

我們同樣可以渲染亂七八糟的標(biāo)簽!

  1. // index.vue改造 
  2.  { 
  3.       render: (h: CreateElement) => 
  4.         h( 
  5.           "h1"
  6.           { 
  7.             style: { color: "green", marginRight: "5px" }, 
  8.           }, 
  9.           "我要去京東" 
  10.         ), 
  11.     }, 

我們可以隨心所欲的在渲染函數(shù)中定義事件:

  1. // index.vue 
  2. private list = [ 
  3.    { 
  4.       render: (h: CreateElement) => 
  5.         h( 
  6.           "a"
  7.           { 
  8.             style: { color: "red", marginRight: "5px" }, 
  9.             on: { 
  10.               click: () => this.iWillGoWhere("TB"), 
  11.             }, 
  12.           }, 
  13.           "我要去淘寶" 
  14.         ), 
  15.    }] 
  16.     
  17.  iWillGoWhere(type: string) { 
  18.     const goWhere: any = { 
  19.       TB: () => { 
  20.         alert("我要去淘寶!"); 
  21.       }, 
  22.       JD: () => { 
  23.         alert("我要去京東!"); 
  24.       }, 
  25.       BD: () => { 
  26.         alert("我要去百度!"); 
  27.       }, 
  28.     }; 
  29.     goWhere[type](); 
  30.   } 

這樣就可以啦!

結(jié)尾

本次文章是對(duì)vue靈活性使用的入門(mén)。請(qǐng)各位vue大佬不要噴我~

責(zé)任編輯:張燕妮 來(lái)源: SegmentFault
相關(guān)推薦

2023-09-14 08:46:50

ReactVue

2022-05-09 08:55:52

ORMMockGo

2021-07-29 09:07:44

React視圖庫(kù)Web 開(kāi)發(fā)

2018-12-04 10:24:23

VueReactJQuery

2017-10-14 22:45:55

前端

2020-10-30 12:40:04

Reac性能優(yōu)化

2024-05-08 16:22:37

2023-04-26 08:43:28

GoCGO語(yǔ)言

2022-06-28 10:58:48

協(xié)議通信加密

2020-01-09 15:35:54

ReactAngularVue.js

2023-10-26 07:37:18

ReactVue項(xiàng)目

2022-12-29 20:23:43

VueReact

2020-09-14 14:18:05

Vue和React

2018-02-26 06:53:21

軟件程序員數(shù)字化

2022-08-21 09:41:42

ReactVue3前端

2017-01-17 18:30:23

ReactVueWeex

2017-01-17 09:39:06

ReactWeex

2021-08-04 08:27:00

VueReact自動(dòng)化部署

2017-06-30 12:53:50

Javascript框架Vue vs Reac

2023-01-27 11:13:04

WebReactVue
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)