4

在 vue 中使用 jsx 与 class component 的各种姿势

 2 years ago
source link: https://blog.guowenfh.com/2019/09/17/2019/vue-jsx-class-component/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在 vue 中使用 jsx 与 class component 的各种姿势

发表于2019-09-17更新于2020-03-12字数统计992阅读时长8分

在之前我们分享过一次 一个使用 react 的思想去使用 vue 的方式
随着组内很多时候为了让 view 层更加清晰,和一些复杂的逻辑处理,导致现在 vue 代码中 jsx 的代码越来越多,这里进行一个整理说明

先参看腾讯 alloyTeam 这篇文章:

里面有提到使用 babel-plugin-transform-vue-jsx babel 6 插件来处理 jsx 的编译。

当然可能是官方也知道在一定的场景下 jsx 相对模板是有优势的,于是单独有了这个仓库 对于上面的插件进行了增强。https://github.com/vuejs/jsx 在 babel 7+ 情况下可以参考使用

npm install @vue/babel-preset-jsx @vue/babel-helper-vue-jsx-merge-props
<!--.babelrc-->
{
"presets": ["@vue/babel-preset-jsx"]
}

你可以在 jsx 中使用 v-model 进行双向绑定了!当然这只是一个语法糖。你也可以使用 babel 实现 v-for 。

对于一些简单的情况我们直接使用 jsx 替换 template 都不会有什么问题,但是当我们深入下去,比如要看一些 react 的 特殊模式 比如:render props 在 vue 中的使用。那么我们就要对 vue 实例的属性差异进行深入的对比和理解了。(render props 在vue中对应的就是 slotProps.default )

与组件库结合的问题

对于 antd-vue 来说,由于 实现的api基本和 react 版本一致,所以调用方式基本和react版本的文档也一致。

import {Input} form 'ant-design-vue'
<Input value={xx} onChange={(e)=>{}}>

//----

const HelloWorld = ({ props }) => <p>hello {props.message}</p>

但是也有一些没有那么友好的组件库, 比如 iview ,由于 内部大部分api都使用了 this.$emit('on-xxEvent') 的形式,在 template 语法下 @on-xxEvent="xx"觉得还好,但是在 jsx 语法下就显得很奇怪了。如下:

<Input value={xx} on-on-Change={(e)=>{}}>

在上面我们处理完了直接使用 jsx 的问题。那么我们能不能更像 react 一点呢?

单文件组件

这个时候我们可能写的一个 vue 单文件组件是这样的:

VueForm.vue

<script>
export default {
name: 'VueForm',
props: {},
data() {
return {}
},
render(){
return (
<div>
<input />
</div>
)
}
}
</script>
<style ></style>

如何直接使用 .js 或者 jsx 文件?

VueForm.jsx

const VueForm = {
name: 'VueForm',
props: {},
data() {
return {}
},
methods:{

},
render(){
return (
<div>
<input />
</div>
)
}
}
VueForm.install = function(Vue) {
Vue.component(VueForm.name, VueForm);
};

export default VueForm;

还是好麻烦啊,每一个组件都的去定义 install 方法,也得去写 methods 啥的,那么如何 再像一些呢?或者说更简单一些呢?

class component

vue 官方提供了 vue-class-component 模块 结合我们上面聊的,我们可以写出来这样的代码。

import Vue from 'vue'
import Component from 'vue-class-component'

@Component({
props: {
propMessage: String
}
})
export default class App extends Vue {
// initial data
msg = 123
// use prop values for initial data
helloMsg = 'Hello, ' + this.propMessage
// lifecycle hook
mounted () {
this.greet()
}
// computed
get computedMsg () {
return 'computed ' + this.msg
}
// method
greet () {
alert('greeting: ' + this.msg)
}
render(){
return (
<input vModel={this.msg}>
<p>prop: {this.propMessage}</p>
<p>msg: {this.msg}</p>
<p>helloMsg: {this.helloMsg}</p>
<p>computed msg: {this.computedMsg}</p>
<button onClick={this.greet}>Greet</button>
)
}
}

当然仅仅是这样可能还是不够的 。你需要再来一个模块 vue-property-decorator 甚至是 vuex-class

哈? 这不是 React + Mobx ?

我们可以看到 vue 的可扩展性是非常强的。恭喜你已经成功进入邪教。23333


Recommend

  • 79
    • www.w3ctech.com 5 years ago
    • Cache

    vue jsx 不完全指北

    Vue 作为日前最为火热的前端框架之一,其流行成都很大部分得益于对开发者友好。尤其是SFC(单文件组件)的模式深得人心,开发者通过在一个文件里同时书写模板,JS 逻辑以及样式,就能完成组件的封装,相比其他方式,组件更加内聚,便于维护...

  • 4
    • www.zoo.team 3 years ago
    • Cache

    拥抱 Vue 3 系列之 JSX 语法

    拥抱 Vue 3 系列之 JSX 语法 2020-07-05 发布于 基础知...

  • 21
    • www.cnblogs.com 3 years ago
    • Cache

    谈谈 Vue 模板和 JSX

    工具链 从学习曲线角度来讲,结合我个人体会,React 学习路线是比 Vue 陡峭的,这个和 JSX、Template 有关吗?当然有。在 React 中使用 JSX,众所周知, JSX 需要经过 Transform 才能在浏览器中运行。马上就有小伙伴反驳了,Vu...

  • 8
    • misaka.im 3 years ago
    • Cache

    JSX 在 Vue 项目上的应用

    JSX 在 Vue 项目上的应用 2019.06.22默认分类 0 评...

  • 6
    • areknawo.com 3 years ago
    • Cache

    Tailwind JSX and class composition

    After my initial look at Tailwind CSS, I haven’t used it much. Like I’ve stated in my previous blog posts, my fee...

  • 9
    • egoist.moe 2 years ago
    • Cache

    Vue JSX 使用指南

    Sep 21, 2017 Vue JSX 使用指南什么是 JSX?没怎么使用 React 或类似框架的同学可能不太了解 JSX,对此我不做赘述,简而言之这就是一种对 JavaScript 的补充,用来描述组件的 UI 部分,类似模板语言但它完整支持 JavaScr...

  • 3
    • juejin.cn 2 years ago
    • Cache

    探索 Vue 3 中的 JSX

    2021年05月22日 阅读 8765 探索 Vue 3 中的 JSX 研发 @ 字节跳动 今天(5月22日...

  • 7

    Nick Scialli • June 14, 2021 • 🚀 3 minute readSometimes we want to have content in React be wrapped in another component, and sometimes we don’t. In this post, we create a Wrapper th...

  • 22

    The React Scheduler allows defining the callout (bubble) content as a React JSX component. The component will be created automatically when the callout is activated and removed and unmounted when the callout is hidden. ...

  • 6
    • share.transistor.fm 2 years ago
    • Cache

    90: David Hemphill - Using JSX with Vue.js

    Using JSX with Vue.js Full Stack Radio

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK