vue 和 react 学习 异同点

本文不做两个框架比较,只对比了两个框架的语法对比,不代表任何观点,盗版必究,本人唯一qq:421217189 欢迎大家一起来学习探讨,壮我大前端(本文markdown直接生成,没有排版,也是为了防止那些不要脸的直接复制我文章的人,在此特声明,所有直接窃取文化知识的人,本人必将追究法律责任,本人qq:421217189,新增一个qq专为大家提供技术:15274527。)

先说一下两个框架的中文文档

Vue

react

可以点击直接前往

安装

vue

$ vue init webpack my-project $ cd my-project $ npm install $ npm run dev

react

$ create-react-app my-app $ cd my-app/ $ npm start

修改端口

vue

/config/index.js/

react

/package.json

hellow world 展示实例生成

vue

import Vue from 'vue' new Vue({ el: '#app', template:` <h1> hello world </h1> ` })

react

import React from 'react' import ReactDOM from 'react-dom' ReactDOM.render( <h1>hello world</h1>, document.getElementById('root') )

dev 运行方式

vue

  • npm run dev

react

  • npm start

变量的使用

vue

import Vue from 'vue' new Vue({ el: '#app', data(){ return { msg: 'hello world', } }, template:` <h1> {{ msg }} </h1> ` })

react

import React from "react"; import ReactDOM from "react-dom"; class State extends React.Component{ constructor(){ super(); this.state={ msg:'this is react' } } render(){ return( <div>{this.state.msg}</div> ) } } ReactDOM.render( <State/>, document.getElementById('root') )

组件props的使用

vue

main.js
import Vue from 'vue' import app from './components/app.vue' new Vue({ el: '#app', components:{ app, }, template:'<app zhangsan="zhangsan"/>' })

app.vue

<template> <h1>{{zhangsan}}</h1> </template> <script> export default { props:[ 'zhangsan' ], data(){ return { msg:'hello world' } } } </script> <style> </style>

react

index.js

import React from "react"; import ReactDOM from "react-dom"; import Dome from "./reactDome.js" ReactDOM.render( <Dome msg = "this is react"/>, document.getElementById('root') )

reactDome.js

import React from "react"; class State extends React.Component { constructor(props) { super(props); } render() { return ( <div>{this.props.msg}</div> ) } } export default State;

组件props默认值、验证

react

import React from "react"; import ReactDOM from "react-dom"; class State extends React.Component { static defaultProps = { msg: 'I am react defaultProps' } constructor(props) { super(props); } render() { return ( <div>{this.props.msg}</div> ) } } ReactDOM.render( <State/>, document.getElementById('root') )

vue

app.vue

<template> <div>{{ msg }}</div> </template> <script> export default { props: { msg: { type: Number, default: 0, } } } </script>

main.js

import Vue from 'vue' Vue.config.productionTip = false import App from '../src/components/app.vue' /* eslint-disable no-new */ new Vue({ el: '#app', template: '<App msg="dsadsda"/>', components: { App } })

class和style的赋值

react

import React from "react"; import ReactDOM from "react-dom"; require('./App.css') class From extends React.Component{ constructor(){ super(); this.state = { className : 'mystyle' } } render() { let style = { color:'#333', fontSize:50, } return( <div> <p class={this.state.className}>11111</p> <p style={style}>2222</p> </div> ) } } ReactDOM.render( <From></From>, document.getElementById('root') );

vue

<template> <div> <p :class="className"> class动态 </p> <p :style = "mystyle"> style动态 </p> </div> </template> <script> export default { data(){ return { className : 'mystyle', mystyle : { color:'#333', fontSize:'50px', } } } } </script> <style> .mystyle{ color:#f00; } </style>
  • 在这里有一点要记住,react可以自动增加px尾缀,vue不会

事件

react

import React from "react"; import ReactDOM from "react-dom"; class App extends React.Component { constructor(){ super(); this.clickfunc = this.clickfunc.bind(this); this.state = { tar : true, } } clickfunc(){ this.setState({ tar:!this.state.tar }) } render() { return ( <div onClick={this.clickfunc}>{this.state.tar?'点我':'再点我'}</div> ) } } ReactDOM.render( <App></App>, document.getElementById('root') )

vue

<template> <button @click="clickfunc">{{ msg }}</button> </template> <script> export default { data(){ return { msg : '点我', tar : true, } }, methods:{ clickfunc(){ this.msg = this.tar?'在点我':'点我'; this.tar = !this.tar; } } } </script>

表单双向绑定

react

import React from "react"; import ReactDOM from "react-dom"; class Module extends React.Component { constructor(){ super(); this.state = { value:'react is good', }; this.changeFrom = this.changeFrom.bind(this); } changeFrom(event){ this.setState({ value : event.target.value }) } render(){ return ( <div> <input value = {this.state.value} onChange = {this.changeFrom}/> <p>{this.state.value}</p> </div> ) } } ReactDOM.render( <Module></Module>, document.getElementById('root') );

vue

<template> <div> <input v-model="value"/> <p>{{ value }}</p> </div> </template> <script> export default { data(){ return { value:"vue is good" } } } </script> <style> </style>

条件渲染模版

react

import React from "react"; import ReactDOM from "react-dom"; class Module1 extends React.Component{ render(){ return ( <div>我是模版1</div> ) } } class Module2 extends React.Component { render() { return ( <h1>我是模版2</h1> ) } } class Module extends React.Component{ constructor(){ super(); this.state = { tar : true } this.changeModule = this.changeModule.bind(this); } componentWillUpdate(){ console.log(this.state.tar) } changeModule(){ this.setState(s => ({ tar : !s.tar })) // console.log(this.state.tar); } render(){ if(this.state.tar){ return ( <div> <button onClick={this.changeModule}>切换模版</button> <Module1/> </div> ) }else{ return ( <div> <button onClick={this.changeModule}>切换模版</button> <Module2 /> </div> ) } } } ReactDOM.render( <Module/>, document.getElementById('root') )

vue

App.vue

<template> <div> <div v-if="show"> 我是模版一 </div> <div v-if="!show"> 我是模版二 </div> <button @click="changeModule">模版切换</button> </div> </template> <script> export default { data(){ return { show:false } }, methods:{ changeModule(){ this.show = !this.show } } } </script> <style> </style>

mainjs

import Vue from 'vue' Vue.config.productionTip = false import App from './App.vue' /* eslint-disable no-new */ let cpt=new Vue({ el: '#app', template: '<App/>', components: { App }, })

列表渲染

react

这里写了一个点击按钮增加一个的方法,也不知道当时怎么想的 反正就是写了不需要的小伙伴删了就好

import React from "react"; import ReactDOM from "react-dom"; class Module extends React.Component { constructor(props) { super(props); } render() { var listtpl = this.props.listarr.map((todo, index) => <li key={index}>{todo}</li> ) return ( <ul>{listtpl}</ul> ) } } class AddModule extends React.Component{ constructor(){ super(); this.state={ listarr: [1, 2, 3, 4, 6, 7], } this.add=this.add.bind(this); } add(){ this.setState(s =>({ listarr: s.listarr.concat([this.state.listarr.length+2]) })) console.log(this.state.listarr) } render(){ return ( <div> <Module listarr={this.state.listarr}/> <button onClick = {this.add} >添加</button> </div> ) } } ReactDOM.render( <AddModule/>, document.getElementById('root') )

vue

<template> <ul> <li v-for="(i,k) in arr">{{k+' => '+i}}</li> </ul> </template> <script> export default { data(){ return { arr:[1,2,3,5,6,7] } } } </script>

组件声明周期函数

react

import React from "react"; import ReactDOM from "react-dom"; import { setInterval } from "timers"; class State extends React.Component { static defaultProps = { msg: 'I am react defaultProps' }; //项目一开始调用 constructor(props) { super(props); this.state = { msg:11, msg2: props.msg } this.func = this.func.bind('this') } func(){ console.log(1); } //在初始化渲染执行之前立刻调用 componentWillMount(){ console.log('组件将要挂载'); //在这里使用setstate会让实例使用这个state this.setState({ msg: 'react componentWillMount' }); } //在初始化渲染执行之后立刻调用一次, componentDidMount(){ console.log('组件已经挂载完成'); } //组件更新时候调用 componentWillReceiveProps(){ console.log('组件props更改了'); this.setState({ msg2 : this.props.msg }) } shouldComponentUpdate(){ console.log('组件props更改了,即将渲染了') //需要提供返回值为true或者false false就不渲染了 return true; // return false; } //在shouldComponentUpdate之后 componentWillUpdate(){ console.log('现在有一个props或者state更新了') } componentDidUpdate(){ console.log('props更新完毕了') } componentWillUnmount(){ console.log('组件从dom移除了'); } render() { return ( <div>{this.state.msg +'+++++++' +this.state.msg2}</div> ) } } class App extends React.Component{ constructor(){ super(); this.state = { msg:'hellow', }; this.handleChange = this.handleChange.bind(this); } handleChange(event) { this.setState({ msg: event.target.value }); } render(){ return ( <div> <input type="text" value={this.state.msg} onChange={this.handleChange} /> <State msg = {this.state.msg}/> </div> ) } } ReactDOM.render( <App/>, document.getElementById('root') )

vue

<template> <div> <p>Data内容{{ msg }}</p> <input type="text" v-model.lazy="msg"> </div> </template> <script> export default { data(){ return { msg:1, } }, beforeCreate(){ console.log('组件实例话'); }, beforeMount(){ console.log('在挂载完成之后被调用'); }, created(){ console.log('组件挂载完成之后被调用,但是比beforeMount更早一点'); }, mounted(){ this.$nextTick(function () { console.log('推荐在nextTick里面走函数,防止子组件没挂载完成'); }) console.log(this.$el); }, beforeUpdate(){ console.log('数据更新开始了,'+this.msg); }, updated(){ console.log('数据更新完成了') }, beforeDestroy(){ console.log('实例销毁之前之前调用') }, beforeDestroy(){ console.log('Vue 实例销毁后调用') } } </script> <style> </style>

vue 和 react 学习 异同点的更多相关文章

  1. Vue与React的异同

    众所周知,前端现在最火的两个框架是Vue和React了.通过一段时间的学习与项目上的实践,我想通过比较他们之间的异同点来发现以后在项目的技术选型中知道怎么抉择用哪个.有一点说明的是他们各自有自己的优势 ...

  2. Vue与React的异同 -生命周期

    vue的生命周期 创建前 beforeCreate 创建   create 挂载前 beforeMount 挂载 mounted 更新前 beforeUpdate 更新 updated 销毁前 bef ...

  3. 谈谈我对前端组件化中“组件”的理解,顺带写个Vue与React的demo

    前言 前端已经过了单兵作战的时代了,现在一个稍微复杂一点的项目都需要几个人协同开发,一个战略级别的APP的话分工会更细,比如携程: 携程app = 机票频道 + 酒店频道 + 旅游频道 + ..... ...

  4. vue入门 vue与react和Angular的关系和区别

    一.为什么学习vue.js vue.js兼具angular.js和react的优点,并且剔除了他们的缺点 官网:http://cn.vuejs.org/ 手册:http://cn.vuejs.org/ ...

  5. 从零开始学习Vue.js,学习笔记

    一.为什么学习vue.js methods 只有纯粹的数据逻辑,而不是去处理 DOM 事件细节. vue.js兼具angular.js和react的优点,并且剔除了他们的缺点 官网:http://cn ...

  6. Vue和React对比

    Vue和React对比 Vue也已经升级到2.0版本了,到现在为止(2016/11/19)比较流行的MVVM框架有AngularJS(也有人认为其为MVC).ReactJS和VueJS,这三个框架中, ...

  7. React学习总结(一)

    React学习总结 一.什么是React? 是Facebook公司开发的一套JS库 React的详细介绍https://www.jianshu.com/p/ae482813b791 二.老版本Reac ...

  8. Vue-起步篇:Vue与React、 Angular的区别

    毋庸置疑,Vue.React. Angular这三个是现在比较火的前端框架.这几个框架都各有所长,选择学习哪种就得看个人喜好或者实际项目了.相比之下, Vue 是轻量级且容易学习掌握的. 1.Vue和 ...

  9. react学习一篇就够了

    webstrom自动格式化代码 命令 js框架 MVC 安装 npm install create-react-app -g 生成项目(项目名npm发包包命名规范 /^[a-z0-9_-]$/) cr ...

随机推荐

  1. SSM框架的搭建

    第一阶段: 1.用PowerDesign建数据模型,并导出SQL文件: 2.将SQL文件导入到MySQL客户端,建立表格: MySQL数据远程访问:GRANT ALL PRIVILEGES ON *. ...

  2. Web Mining and Big Data 公开课学习笔记 ---lecture1

    1.1  LOOK Finding "stuff" on the web or computer or room or hidden in data Finding documen ...

  3. Libevent 反应堆的初始化

    // 默认情况下new eventbase struct event_base * event_base_new(void) { struct event_base *base = NULL; //初 ...

  4. MySQL slave_exec_mode 参数说明

    背景: 今天无意当中看到参数slave_exec_mode,从手册里的说明看出该参数和MySQL复制相关,是可以动态修改的变量,默认是STRICT模式(严格模式),可选值有IDEMPOTENT模式(幂 ...

  5. snprintf 返回值陷阱 重新封装

    snprintf()函数用于将格式化的数据写入字符串,其原型为: int snprintf(char *str, int n, char * format [, argument, ...]); st ...

  6. echarts分组插件echarts.group代码分享

    前言 echarts是百度出品的一款很棒的前端图表控件,被评为"百度少有的良心产品".可以实现散点图.折线图.柱状图.地图.饼图.雷达图.K线图等等几十种常用.不常用的图表,效果酷 ...

  7. python 正则的使用 —— 编写一个简易的计算器

    在 Alex 的博客上看到的对正则这一章节作业是编写一个计算器,要求能计算出下面的算式. 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 + ...

  8. vue页面切换效果(slide效果切换)

    最近碰到一个需求,单页应用里面页面切换的效果需要做成跟轮播图滑动slide一样,让这个页面在切换时感觉是一个页面.反复琢磨的vue里面的transition,最终将实现的核心代码贴出来.这里实现的是上 ...

  9. 利用qq设置个性化的域名邮箱

    首先有域名.先去注冊先,国内有非常多域名交易平台.我是在易名中国网上注冊的一个域名,Lios.top,因为这个域名价值非常低.一年才 10元,价格非常实惠. 下一步就是去qq邮箱的设置里,找到账户,然 ...

  10. EBS採购模块中的级联接收和级联接收事务

    EBS採购模块中的级联接收和级联接收事务 (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习.请注明出处:否则请与本人联系.违者必究) 级联接收和级联接收事务 级联功能对来自于同一个供应商 ...