博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
react 与vue开发中常见的不同(持续整理中...)
阅读量:2120 次
发布时间:2019-04-30

本文共 4498 字,大约阅读时间需要 14 分钟。

引入图片和背景

react:

import logo from './logo.svg';...logo

vue:

logo
背景
...import logo from './logo.svg';...data: function () { return { logo:logo,//必要 } },

动态添加className

不同于常规的calss写法,react中要写"className"

react:

添加className
添加更多className
{/*动态给子组件的class传值*/}

vue:

添加更多class
Demo7
//计算属性
... data: function () { return { classA:'noBorder', classB:'activedTheme', isActive: true, } }, computed: { classObject: function (i) {//计算属性传值 return function(i) { console.log(i); return {} }, coinClassObj:function(i){ return function(i) { var classN='coinClass'+(i+1); var obj={ 'coinItem2':i%2==0, 'coinItem1':i%2!=0, 'hiddenCoin':i>=5, }; obj[classN]=true; return obj } } }

组件生命周期

vue:根实例在main.js中被创建:

var app=new Vue({el:"#app",components:{APP},template:’’})

然后项目中的vue文件,一个vue文件可看做一个组件,这些可嵌套可复用的组件与根实例组成一个vue应用。

实例(组件)钩子:

在这里插入图片描述

执行结果:

在这里插入图片描述
当改变data时触发beforeUpdate updated函数,其他的函数都只是调用一次,当需要监听某个data改变时,可使用watch。
react:
在这里插入图片描述
现在看一下执行:

import React,{Component} from 'react'class Home extends Component {    constructor(props) {//getInitialState()        super(props);        this.state = { flag: 'data数据' };    }    componentWillMount(){        console.log('componentWillMount-----------');        console.log(this.state.flag);    }    render() {        console.log('render-----------');        console.log(this.state.flag);        return (            
Home
) } componentDidMount(){ console.log('componentDidMount-----------'); console.log(this.state.flag); console.log(document.getElementById('box')); } componentWillReceiveProps(){ console.log('componentWillReceiveProps-----------'); } shouldComponentUpdate(){ console.log('shouldComponentUpdate-----------'); } componentWillUpdate(){ console.log('componentWillUpdate-----------'); } componentDidUpdate(){ console.log('componentDidUpdate-----------'); } componentWillUnmount(){ console.log('componentWillUnmount-----------'); }}export default Home

在这里插入图片描述

看出组件初始化时,componentDidMount后面的函数不会触发,
当state改变时,如果有shouldComponentUpdate会先触发这个函数,没有则继续下面图的:
在这里插入图片描述
切换路由时,离开当前组件:
在这里插入图片描述
当改变state时触发componentWillUpdate,render,componentDidUpdate函数,其他的函数都只是调用一次。

this指向

vue中,生命周期钩子及事件函数中的this即当前的vue对象

例 <span @click=‘closeThis’>关闭

closeThis:function(){      console.log(this)  //VueComponent}

react要做处理,否则事件中的this为undefined,或者用es6写法

例:< span onClick={this.closeThis}>关闭

closeThis(){      console.log(this)  //undefined}

例:< span onClick={this.closeThis.bind(this)}>关闭

closeThis(){      console.log(this)  // Home [即当前的react对象]}

es6写法:

< span onClick={this.closeThis}>关闭

handleChange=(event)=>{    console.log(this);//Home}

全局组件

场景:需要封装一堆的自定义组件,一次引入,全局使用

vue:
配置:main.js(一次引入)

//elementUI引入import ElementUI from 'element-ui'Vue.use(ElementUI);

使用:(任意地方)

react使用(某jsx中):

import BottomNavigation from '@material-ui/core/BottomNavigation';...

React不能使用vue的上面的方式,只能使用一次,单独import目标组件;(这个答案不绝对啊,只是现在我这里是这样的,确实找不到方法了,希望有更好的办法的朋友评论告诉一下技巧,谢谢)

组件的命名

react组件的命名必须是大写开头,但是vue不是。

import书写位置

react使用import时要注意,在import前不能出现非import语句,否则报错。

组件内样式的书写

react:

//样式的书写格式应该是键值对的方式,属性名为驼峰var style={    banner:{        fontSize:"32px",        color:"red"    }}class Home extends Component {    render() {        return (            
324
) }}

vue

...

vue渲染html数据,React渲染html数据

vue :< div v-html=“htmlData”></div>

React:< div dangerouslySetInnerHTML={
{__html: htmlData}}></div>

动态渲染自定义组件(vue 的component :is 在react中的实现方式)

vue:

... components: { 'module101':Com_img, 'module102':Com_nav,}

react:

drawCom=(name,listData)=>{        switch (name) {            case 'module101':                return 
; default: break; } }...render() { return (
{ this.state.modules.map(item=>( this.drawCom(moduleCode,item) )) }
) }

react多行省略失效

审查样式发现在编译后没有用于设置多行省略的样式-webkit-box-orient: vertical;解决方式:

/* autoprefixer: ignore next */    -webkit-box-orient: vertical;

在 -webkit-box-orient: vertical 上方加过滤;

转载地址:http://viurf.baihongyu.com/

你可能感兴趣的文章
压力测试工具JMeter入门教程
查看>>
作为一名软件测试工程师,需要具备哪些能力
查看>>
【Pyton】【小甲鱼】类和对象:一些相关的BIF(内置函数)
查看>>
【Pyton】【小甲鱼】魔法方法
查看>>
单元测试需要具备的技能和4大阶段的学习
查看>>
【Loadrunner】【浙江移动项目手写代码】代码备份
查看>>
Python几种并发实现方案的性能比较
查看>>
[Jmeter]jmeter之脚本录制与回放,优化(windows下的jmeter)
查看>>
Jmeter之正则
查看>>
【JMeter】1.9上考试jmeter测试调试
查看>>
【虫师】【selenium】参数化
查看>>
【Python练习】文件引用用户名密码登录系统
查看>>
学习网站汇总
查看>>
【Python】用Python打开csv和xml文件
查看>>
【Loadrunner】性能测试报告实战
查看>>
【面试】一份自我介绍模板
查看>>
【自动化测试】自动化测试需要了解的的一些事情。
查看>>
【selenium】selenium ide的安装过程
查看>>
【手机自动化测试】monkey测试
查看>>
【英语】软件开发常用英语词汇
查看>>