vue、react等单页面项目部署到服务器的方法及vu(2)

/** * 疑惑一: * React createClass 和 extends React.Component 有什么区别? * 之前写法: * let app = React.createClass({ * getInitialState: function(){ * // some thing * } * }) * ES6写法(通过es6类的继承实现时state的初始化要在constructor中声明): * class exampleComponent extends React.Component { * constructor(props) { * super(props); * this.state = {example: 'example'} * } * } */ import React, {Component, PropTypes} from 'react'; // react核心 import { Router, Route, Redirect, IndexRoute, browserHistory, hashHistory } from 'react-router'; // 创建route所需 import Config from '../config/index'; import layout from '../component/layout/layout'; // 布局界面 import login from '../containers/login/login'; // 登录界面 /** * (路由根目录组件,显示当前符合条件的组件) * * @class Roots * @extends {Component} */ class Roots extends Component { render() { // 这个组件是一个包裹组件,所有的路由跳转的页面都会以this.props.children的形式加载到本组件下 return ( <div>{this.props.children}</div> ); } } // const history = process.env.NODE_ENV !== 'production' ? browserHistory : hashHistory; // 快速入门 const home = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/home/homeIndex').default) }, 'home'); } // 百度图表-折线图 const chartLine = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/charts/lines').default) }, 'chartLine'); } // 基础组件-按钮 const button = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/general/buttonIndex').default) }, 'button'); } // 基础组件-图标 const icon = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/general/iconIndex').default) }, 'icon'); } // 用户管理 const user = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/user/userIndex').default) }, 'user'); } // 系统设置 const setting = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/setting/settingIndex').default) }, 'setting'); } // 广告管理 const adver = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/adver/adverIndex').default) }, 'adver'); } // 组件一 const oneui = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/ui/oneIndex').default) }, 'oneui'); } // 组件二 const twoui = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/ui/twoIndex').default) }, 'twoui'); } // 登录验证 const requireAuth = (nextState, replace) => { let token = (new Date()).getTime() - Config.localItem('USER_AUTHORIZATION'); if(token > 7200000) { // 模拟Token保存2个小时 replace({ pathname: '/login', state: { nextPathname: nextState.location.pathname } }); } } const RouteConfig = ( <Router history={browserHistory}> <Route path="/home" component={layout} onEnter={requireAuth}> <IndexRoute getComponent={home} onEnter={requireAuth} /> // 默认加载的组件,比如访问,会自动跳转到 <Route path="/home" getComponent={home} onEnter={requireAuth} /> <Route path="/chart/line" getComponent={chartLine} onEnter={requireAuth} /> <Route path="/general/button" getComponent={button} onEnter={requireAuth} /> <Route path="/general/icon" getComponent={icon} onEnter={requireAuth} /> <Route path="/user" getComponent={user} onEnter={requireAuth} /> <Route path="/setting" getComponent={setting} onEnter={requireAuth} /> <Route path="/adver" getComponent={adver} onEnter={requireAuth} /> <Route path="/ui/oneui" getComponent={oneui} onEnter={requireAuth} /> <Route path="/ui/twoui" getComponent={twoui} onEnter={requireAuth} /> </Route> <Route path="/login" component={Roots}> // 所有的访问,都跳转到Roots <IndexRoute component={login} /> // 默认加载的组件,比如访问,会自动跳转到 </Route> <Redirect from="*" to="/home" /> </Router> ); export default RouteConfig;

vue、react等单页面项目部署到服务器的方法及vu

############ # 其他配置 ############ http { ############ # 其他配置 ############ server { listen 80; server_name antd.sosout.com; root /mnt/html/reactAntd; index index.html; location ~ ^/favicon\.ico$ { root /mnt/html/reactAntd; } location / { try_files $uri $uri/ @router; index index.html; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } location @router { rewrite ^.*$ /index.html break; } access_log /mnt/logs/nginx/access.log main; } ############ # 其他配置 ############ }

下面看下vue和react区别

前端都知道3个主流框架,vue,react,anjular,当然目前最火的还是vue和react,那么vue 和react 的区别?

相同点:

1.都支持服务器端渲染

2.都有Virtual DOM,组件化开发,通过props参数进行父子组件数据的传递,都实现webComponent规范

3.数据驱动视图

4.都有支持native的方案,React的React native,Vue的weex

5.都有管理状态,React有redux,Vue有自己的Vuex(自适应vue,量身定做)

不同点:

1.React严格上只针对MVC的view层,Vue则是MVVM模式

2.virtual DOM不一样,vue会跟踪每一个组件的依赖关系,不需要重新渲染整个组件树.

而对于React而言,每当应用的状态被改变时,全部组件都会重新渲染,所以react中会需要shouldComponentUpdate这个生命周期函数方法来进行控制

3.组件写法不一样, React推荐的做法是 JSX + inline style, 也就是把HTML和CSS全都写进JavaScript了,即'all in js';

Vue推荐的做法是webpack+vue-loader的单文件组件格式,即html,css,jd写在同一个文件;

4.数据绑定: vue实现了数据的双向绑定,react数据流动是单向的

5.state对象在react应用中不可变的,需要使用setState方法更新状态;

在vue中,state对象不是必须的,数据由data属性在vue对象中管理;

就对我而言吧,vue适合开发移动端项目,react适合开发pc端项目(个人观点),

当然我还是喜欢 React,毕竟后台大,哈哈,虽然现在升级到16版本了(不喜勿喷)

总结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/25b0e6dceefc1537f79c503110c948c9.html