君陌离的博客

vuePress-theme-reco 君陌离    2018 - 2020
君陌离的博客 君陌离的博客

Choose mode

  • dark
  • auto
  • light
首页
我的作品
  • 项目橱窗
  • blog模板
分类
  • 数据库
  • CSS
  • 摘记
  • JS
  • Node
  • Vue
  • React
  • GIT
  • Promise
  • Liunx
  • Xshell
  • ajax
  • WINDOWS
  • Python
  • 随笔
  • 脚手架
  • node
  • 自动化
标签
笔记
时间线
About Me
  • 关于我
  • 赞赏
Contact
  • GitHub
  • QQ
author-avatar

君陌离

70

文章

90

标签

首页
我的作品
  • 项目橱窗
  • blog模板
分类
  • 数据库
  • CSS
  • 摘记
  • JS
  • Node
  • Vue
  • React
  • GIT
  • Promise
  • Liunx
  • Xshell
  • ajax
  • WINDOWS
  • Python
  • 随笔
  • 脚手架
  • node
  • 自动化
标签
笔记
时间线
About Me
  • 关于我
  • 赞赏
Contact
  • GitHub
  • QQ
  • HTML5

  • JS

  • 微信小程序

  • 数据库

  • React

    • 与Vue相比React的指令使用方法
    • React采坑记录
    • react路由跳转、传参3中方式及区别
    • React生命周期执行顺序
  • Vue

  • vuepress

与Vue相比React的指令使用方法

vuePress-theme-reco 君陌离    2018 - 2020

与Vue相比React的指令使用方法

君陌离 2020-08-12 14:44:26 ReactReact指令

# v-text

import React, { Component } from 'react'

export default class App extends Component{
    render(){
        let value = 'hello world';
        return (
            <div>
                {/*v-text*/}
                <p>{value}</p>
            </div>
        )
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# v-if

import React, { Component } from 'react'

export default class App extends Component{
    
    render(){
        let isExist = true;
        return (
            <div>
                {/* v-if */}
                {isExist && <div className="box"></div>}
                {isExist ? <div className="box"></div>:''}
            </div>
        )
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# v-bind

import React, { Component } from 'react'

export default class App extends Component{
    render(){
        let value = 'hello world';
        let path = 'http://www.baidu.com';
        return (
            <div>
                {/* v-bind */}
                <h1 title={value}></h1>
                <a href={path}>百度一下</a>
            </div>
        )
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# class 与 style

import React, { Component } from 'react'

export default class App extends Component{
    render(){
      let isExist = true;
        let classValue1 = "a b c";
        // let classValue2 ='b c' + (isExist?' a':'');
        let classValue2 ='b c ' + (isExist && 'a');
        let styleValue1 = {
            width: '100px',
            height: '100px',
            background: 'red'
        };
        return (
            <div>
                {/* class 自己拼接class的格式   style 写成对象的形式 */}
                <div className={classValue2}></div>
                <div style={styleValue1}>box</div>
                <div style={{width: '100px', height: '50px', background: 'yellow'}}>box</div>
            </div>
        )
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# v-show

import React, { Component } from 'react'

export default class App extends Component{
    render(){
        let isShow = false;
        return (
            <div>
                {/* v-show */}
                <div className="box" style={{display: isShow?'':'none'}}></div>
            </div>
        )
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# v-for

import React, { Component } from 'react'

export default class App extends Component{
    render(){
        let arr = ['a', 'b', 'c', 'd'];
        let obj = {
            a: 1,
            b: 2,
            c: 3
        }
        return (
            <div>
                {/* v-for */}
                <ul>
                    {
                        arr.map((item, index)=>{
                            return <li key={index}>{item}----{index}</li>
                        })
                    }
                </ul>

                <ul>
                    {
                        (function(){
                            let newArr = [];
                            for(let key in obj){
                                newArr.push(
                                    <li key={key}>{key}: {obj[key]}</li>
                                );
                            }
                            return newArr;
                        })()
                    }
                </ul>
                <ul>
                    {
                        Object.entries(obj).map(([key, value])=>{
                            return <li key={key}>{value}</li>
                        })
                    }
                </ul>
            </div>
        )
    }
}
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

# v-on

import React, { Component } from 'react'

export default class App extends Component{
    render(){
        return (
            <div>
                {/* v-on */}
                <button onClick={()=>{
                    console.log('按钮点击了1');
                }}>按钮1</button>

                <button onClick={this.btnAction}>按钮2</button>
            </div>
        )
    }
    btnAction(){
        console.log('按钮点击了2');
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import React, { Component } from 'react'

export default class App extends Component{
    constructor(){
        super();
        this.state = {
            message: 'hello world'
        }
    }
    render(){
        let value = 'hello react';

        return (
            <div>

                {/* v-on */}

                {/* 事件的写法 */}
                <button onClick={()=>{
                    console.log('按钮点击了1:', this.state.message);
                }}>按钮1</button>

                <button onClick={this.btnAction2.bind(this)}>按钮2</button>

                <button onClick={this.btnAction3}>按钮3</button>

                <button onClick={()=>this.btnAction4()}>按钮4</button>

                <br/>
                <br/>
                <br/>

                {/* 事件传参 */}
                <button onClick={(ev)=>{
                    console.log('按钮点击了1:', this.state.message);
                    console.log('按钮点击了1:', value);
                    console.log(ev);
                }}>按钮1</button>

                <button onClick={this.btnAction2.bind(this, value, value, value)}>按钮2</button>

                <button onClick={this.btnAction3}>按钮3</button>{/*不支持传参 */}

                <button onClick={(ev)=>{
                    return this.btnAction4(value, ev);
                }}>按钮4</button>

            </div>
        )
    }

    btnAction2(val1, val2, val3, ev){
        console.log('按钮点击了2: ', this.state.message);
        console.log('按钮点击了2: ', val1);
        console.log(ev);
    }
s
    btnAction3 = ()=>{
        console.log('按钮点击了3: ', this.state.message);
    }

    btnAction4(val, ev){
        console.log('按钮点击了4: ', this.state.message);
        console.log('按钮点击了4: ', val);
        console.log(ev);
    }
}
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
欢迎来到 您的站点名称($site.title)
看板娘