vue 和 react 学习 异同点
本文不做两个框架比较,只对比了两个框架的语法对比,不代表任何观点,盗版必究,本人唯一qq:421217189 欢迎大家一起来学习探讨,壮我大前端(本文markdown直接生成,没有排版,也是为了防止那些不要脸的直接复制我文章的人,在此特声明,所有直接窃取文化知识的人,本人必将追究法律责任,本人qq:421217189,新增一个qq专为大家提供技术:15274527。)
先说一下两个框架的中文文档
可以点击直接前往
安装
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
react
变量的使用
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的异同
众所周知,前端现在最火的两个框架是Vue和React了.通过一段时间的学习与项目上的实践,我想通过比较他们之间的异同点来发现以后在项目的技术选型中知道怎么抉择用哪个.有一点说明的是他们各自有自己的优势 ...
- Vue与React的异同 -生命周期
vue的生命周期 创建前 beforeCreate 创建 create 挂载前 beforeMount 挂载 mounted 更新前 beforeUpdate 更新 updated 销毁前 bef ...
- 谈谈我对前端组件化中“组件”的理解,顺带写个Vue与React的demo
前言 前端已经过了单兵作战的时代了,现在一个稍微复杂一点的项目都需要几个人协同开发,一个战略级别的APP的话分工会更细,比如携程: 携程app = 机票频道 + 酒店频道 + 旅游频道 + ..... ...
- vue入门 vue与react和Angular的关系和区别
一.为什么学习vue.js vue.js兼具angular.js和react的优点,并且剔除了他们的缺点 官网:http://cn.vuejs.org/ 手册:http://cn.vuejs.org/ ...
- 从零开始学习Vue.js,学习笔记
一.为什么学习vue.js methods 只有纯粹的数据逻辑,而不是去处理 DOM 事件细节. vue.js兼具angular.js和react的优点,并且剔除了他们的缺点 官网:http://cn ...
- Vue和React对比
Vue和React对比 Vue也已经升级到2.0版本了,到现在为止(2016/11/19)比较流行的MVVM框架有AngularJS(也有人认为其为MVC).ReactJS和VueJS,这三个框架中, ...
- React学习总结(一)
React学习总结 一.什么是React? 是Facebook公司开发的一套JS库 React的详细介绍https://www.jianshu.com/p/ae482813b791 二.老版本Reac ...
- Vue-起步篇:Vue与React、 Angular的区别
毋庸置疑,Vue.React. Angular这三个是现在比较火的前端框架.这几个框架都各有所长,选择学习哪种就得看个人喜好或者实际项目了.相比之下, Vue 是轻量级且容易学习掌握的. 1.Vue和 ...
- react学习一篇就够了
webstrom自动格式化代码 命令 js框架 MVC 安装 npm install create-react-app -g 生成项目(项目名npm发包包命名规范 /^[a-z0-9_-]$/) cr ...
随机推荐
- Error parsing column 8 (IsRecommended=0 - SByte) Dapper查询mysql数据库可空的tinyint(1)一个错误
出错条件: 1.实体属性为bool?类型 2.对应字段为可空的tinyint(1)类型 3.该字段查询结果内即含有null,又含有正常值 google答案,两种建议: 1.修改sql语句,直接cast ...
- 找到链表的倒数第K位
#include<iostream> using namespace std; class node{ public: node():value(),next(NULL){} ~node( ...
- Android OpenGL ES 开发(一): OpenGL ES 介绍
简介OpenGL ES 谈到OpenGL ES,首先我们应该先去了解一下Android的基本架构,基本架构下图: 在这里我们可以找到Libraries里面有我们目前要接触的库,即OpenGL ES. ...
- 【MySQL疑难杂症】如何将树形结构存储在数据库中(方案一、Adjacency List)
今天来看看一个比较头疼的问题,如何在数据库中存储树形结构呢? 像mysql这样的关系型数据库,比较适合存储一些类似表格的扁平化数据,但是遇到像树形结构这样有深度的人,就很难驾驭了. 举个栗子:现在有一 ...
- 51Nod 1090 3个数和为0 set 二分优化
给出一个长度为N的无序数组,数组中的元素为整数,有正有负包括0,并互不相等.从中找出所有和 = 0的3个数的组合.如果没有这样的组合,输出No Solution.如果有多个,按照3个数中最小的数从小到 ...
- mac下出现xcrun: error导致git、svn无法使用的解决办法
现象:xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun ...
- win10 mysql详尽安装教程
我的电脑系统是win10 64位系统 我安装mysql不下5次,装好了又卸,卸了又装,看了老多篇文章和博客,非常感谢博主的无私帮助,以下是这些博主的文章: https://www.cnblogs.co ...
- 闲来无事研究一下酷狗缓存文件kgtemp的加密方式
此贴为本人原创,转载请注明出处 序 前几天更新了被打入冷宫很久的酷狗,等进入之后就感觉菊花一紧----试 听 居 然 都 要 开 通 音 乐 包(高品和无损)才行了,WTF! 这意味着以前缓存的都听不 ...
- 用KMP算法实现strStr()
strStr()函数的用途是在一个字符串S中寻找某个字串P第一次出现的位置.并返回其下标,找不到时返回-1.最简单的办法就是找出S全部的子串和P进行比較,然而这种方法比較低效.假设我们从S的下标0和P ...
- 基于Handler架构的录音程序
近期我的app须要一个录音功能,于是搜到这篇文章 文章中录音线程与主线程间的通讯是通过内部类訪问外部类成员变量的方式来实现 while (isRecord == true) { //isRecord是 ...