Wang's blog Wang's blog
首页
  • 前端文章

    • HTML教程
    • CSS
    • JavaScript
  • 前端框架

    • Vue
    • React
    • VuePress
    • Electron
  • 后端技术

    • Npm
    • Node
    • TypeScript
  • 编程规范

    • 规范
  • 我的笔记
  • Git
  • GitHub
  • VSCode
  • Mac工具
  • 数据库
  • Google
  • 服务器
  • Python爬虫
  • 前端教程
更多
收藏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Wang Mings

跟随大神,成为大神!
首页
  • 前端文章

    • HTML教程
    • CSS
    • JavaScript
  • 前端框架

    • Vue
    • React
    • VuePress
    • Electron
  • 后端技术

    • Npm
    • Node
    • TypeScript
  • 编程规范

    • 规范
  • 我的笔记
  • Git
  • GitHub
  • VSCode
  • Mac工具
  • 数据库
  • Google
  • 服务器
  • Python爬虫
  • 前端教程
更多
收藏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • CSS

  • Npm

  • Vue

    • Vue简介
    • 路由守卫
    • tsvue的写法
    • Vue常用技巧
    • vue的JSX写法
      • 函数式组件
      • 赋值
      • 定义事件
      • 在 render 中使用
      • Fragment(片段)
      • Attributes / Props
      • 指令
        • v-show
        • v-model
      • 自定义指令
      • 插槽
      • 在 TypeScript 中使用
      • 兼容性
    • vue防抖组件
    • 动态热更新设置
    • keepActive缓存路由
    • 自动生成面包屑VUE组件
    • vue生命周期对比生命周期
    • eslint编译时警告或错误配置
    • Vue中封装axios的取消请求事情
    • vue+element递归生成无限菜单组件
    • Vue框架dist目录下各个文件的区别
    • 超详细Vue的种和Vue的种组件间通信方式
    • Vue项目中出现Loadingchunk{n}failed问题的解决方法
    • Vuex

    • 其他

    • 基础

    • 工具

    • 组件

    • 规模化

    • 过渡&动画

    • 可复用性&组合

  • HTML

  • Node

  • Yaml

  • React

  • 框架

  • 规范

  • Electron

  • JS演示

  • VuePress

  • JavaScript

  • TypeScript

  • 微信小程序

  • TypeScript-axios

  • 前端
  • Vue
wangmings
2022-07-19
目录

vue的JSX写法

# vue3的JSX写法

# 函数式组件

const App = () => <div></div>;
1

# 赋值

const msg = '阿离王'
const App = () => <div> { msg } </div>
1
2

# 定义事件

const showText = (msg) => { console.log(msg) }

const App = (msg) => <div>
    <div onClick={ showText }></div>
    <div onClick={ () => { showText(msg) } }></div>
</div>;
1
2
3
4
5
6

# 在 render 中使用

const App = {
  render() {
    return <div>Vue 3.0</div>;
  },
};
1
2
3
4
5
import { withModifiers, defineComponent } from "vue";

const App = defineComponent({
  setup() {
    const count = ref(0);

    const inc = () => {
      count.value++;
    };

    return () => (
      <div onClick={withModifiers(inc, ["self"])}>{count.value}</div>
    );
  },
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# Fragment(片段)

const App = () => (
  <>
    <span>I'm</span>
    <span>Fragment</span>
  </>
);
1
2
3
4
5
6

# Attributes / Props

const App = () => <input type="email" />;
1

动态绑定:

const placeholderText = "email";
const App = () => <input type="email" placeholder={placeholderText} />;
1
2

# 指令

# v-show

const App = {
  data() {
    return { visible: true };
  },
  render() {
    return <input v-show={this.visible} />;
  },
};
1
2
3
4
5
6
7
8

# v-model

<input v-model={val} />

<input v-model={[val, ["modifier"]]} />

<input v-model:argument={val} />

<A v-model={[val, "argument", ["modifier"]]} />
1
2
3
4
5
6
7

以上四种写法都可以,下面两种 argument 意思是别名

v-model:argument={val}语法糖=> :argument={ argument } @update:argument={ val => argument = val } $emit('update:argument', 123)

<A v-model={[val, "argument", ["modifier"]]} /> 会编译成

h(A, {
  argument: val,
  argumentModifiers: {
    modifier: true,
  },
  "onUpdate:argument": ($event) => (val = $event),
});
1
2
3
4
5
6
7

# 自定义指令

只有 argument 的时候推荐使用

const App = {
  directives: { custom: customDirective },
  setup() {
    return () => <a v-custom:arg={val} />;
  },
};
1
2
3
4
5
6
const App = {
  directives: { custom: customDirective },
  setup() {
    return () => <a v-custom={[val, "arg", ["a", "b"]]} />;
  },
};
1
2
3
4
5
6

# 插槽

注意: 在 jsx 中,应该使用 v-slots 代替 v-slot

const A = (props, { slots }) => (
  <>
    <h1>{ slots.default ? slots.default() : 'foo' }</h1>
    <h2>{ slots.bar?.() }</h2>
  </>
);

const App = {
  setup() {
    const slots = {
      bar: () => <span>B</span>,
    };
    return () => (
      <A v-slots={slots}>
        <div>A</div>
      </A>
    );
  },
};

// or

const App = {
  setup() {
    const slots = {
      default: () => <div>A</div>,
      bar: () => <span>B</span>,
    };
    return () => <A v-slots={slots} />;
  },
};

// 或者,当 `enableObjectSlots` 不是 `false` 时,您可以使用对象插槽
const App = {
  setup() {
    return () => (
      <>
        <A>
          {{
            default: () => <div>A</div>,
            bar: () => <span>B</span>,
          }}
        </A>
        <B>{() => "foo"}</B>
      </>
    );
  },
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

# 在 TypeScript 中使用

tsconfig.json:

{
  "compilerOptions": {
    "jsx": "preserve"
  }
}
1
2
3
4
5

# 兼容性

要求:

  • Babel 7+
  • Vue 3+

官方文档 Vue 3 Babel JSX 插件 (opens new window)

编辑 (opens new window)
Vue常用技巧
vue防抖组件

← Vue常用技巧 vue防抖组件→

最近更新
01
theme-vdoing-blog博客静态编译问题
09-16
02
搜索引擎
07-19
03
友情链接
07-19
更多文章>
Theme by Vdoing | Copyright © 2019-2022 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式