教练,怎么在vue项目里写react?

开发 前端
我承认了我是标题党,本篇文章是在vue项目里写tsx的一篇介绍。作为一个reacter,目前的业务天天使用vue2+ts让我十分的不舒服。

 [[424253]]

1.前言

我承认了我是标题党,本篇文章是在vue项目里写tsx的一篇介绍。作为一个reacter,目前的业务天天使用vue2+ts让我十分的不舒服。我对于vue也不是很熟悉,想回到我的react时代。于是在查询官网之后发现在vue里面写jsx也挺有意思的,遂记录。

2.正文

vue2+ts的项目配置这里就不展开了,网上一搜一大推。

index.vue是页面路由,存放各个组件和公用逻辑。components文件夹中存放我的tsx组件。

接下来就开始写tsx。

你可以直接创建jsx/tsx文件

这次的项目结构是这样的:

在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这么写

  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.     } 

在来看一下页面

这该死的react既视感,竟是如此的诱人

可能有心者注意到了 我还引用了一个 CreateElement ,这是干嘛的呢。这玩意叫 渲染函数 。不喜欢读vue那么大串的文档的兄弟看这里。简单解释:这个东西可以渲染一个vnode节点。 它比模板更接近编译器。 什么意思呢?意思就是模板语法也会编译成渲染函数。所以我们直接用渲染函数不就相当于节省了模板语法到渲染函数的过程。四舍五入项目性能又是一个大的提升!

简单介绍一下传参:

第一个参数 : {String | Object | Function} 一个 HTML 标签名、组件选项对象,或者 resolve 了上述任何一种的一个 async 函数。必填项。

第二个参数 : Object 一个与模板中 attribute 对应的数据对象。

第三个参数 : {String | Array} 文本节点或子级虚拟节点 (VNodes)。

渲染函数给vue带来了很多的灵活性,以前你想自定义在子组件里插入东西,得写一大堆的插槽。 <slot> 。有了渲染函数我们可以这么玩。

  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中这么写:

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

就可以渲染出花里胡哨的页面了

我们还可以这么玩:

  1. // tsx改造 
  2. <span> 
  3.             { 
  4.                 this.opt.map((it) => { 
  5.                     return it.render(h) 
  6.                 }) 
  7.             } 
  8. </span> 
  9.  
  10.  
  11. 在index.vue页面我们就可以这么玩: 
  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.   ]; 

结果也是同样的花哨

我们同样可以渲染乱七八糟的标签!

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

我们可以随心所欲的在渲染函数中定义事件:

  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.   } 

这样就可以啦!

结尾

本次文章是对vue灵活性使用的入门。请各位vue大佬不要喷我~

责任编辑:张燕妮 来源: SegmentFault
相关推荐

2023-09-14 08:46:50

ReactVue

2022-05-09 08:55:52

ORMMockGo

2021-07-29 09:07:44

React视图库Web 开发

2018-12-04 10:24:23

VueReactJQuery

2017-10-14 22:45:55

前端

2022-06-28 10:58:48

协议通信加密

2023-04-26 08:43:28

GoCGO语言

2020-10-30 12:40:04

Reac性能优化

2020-01-09 15:35:54

ReactAngularVue.js

2020-09-14 14:18:05

Vue和React

2023-10-26 07:37:18

ReactVue项目

2022-12-29 20:23:43

VueReact

2018-02-26 06:53:21

软件程序员数字化

2022-08-21 09:41:42

ReactVue3前端

2009-12-18 10:30:55

Fedora Core

2017-01-17 18:30:23

ReactVueWeex

2021-08-04 08:27:00

VueReact自动化部署

2017-01-17 09:39:06

ReactWeex

2022-10-30 17:32:25

设计模式单例模式

2022-09-23 10:25:00

VueReact
点赞
收藏

51CTO技术栈公众号