美文网首页
理想的web组件模式

理想的web组件模式

作者: 方健 | 来源:发表于2017-01-03 17:02 被阅读11次

面向接口编程

写功能之前,先写接口。
前端和后端之间的接口:RESTful API
后端用express的情况下,这个接口又体现为一个中间件
前端用react的情况下,这个接口应当体现为一组JS API
react使用JS API编程。
react组件与组件之间的接口,直接组件用props,间接组件用pubsub。

举例:

//后端 express:
var login=require('login');
app.use('/login',login);
//前端 API:
var login=axios.get('/login');
//前端REACT 组件调用
<Login logIn={login}/>

部分前端功能代码:

//Login组件定义
import React from 'react';

export default class Login extends React.Component {
  static propTypes = {
    login: React.PropTypes.func.isRequired,
    routeSuccess:React.PropTypes.string//成功后去向的路由,默认为login_after
  };

  constructor(props) {
    super(props);
    this.gotoLoginAfter=this.gotoLoginAfter.bind(this);
    this.showError=this.showError.bind(this);
  }

  render() {

    return (
        <form  role="form"  onSubmit={this.handleSubmit.bind(this)}>
            <h2 className="form-signin-heading">请登录</h2>
            <input type="text" className="form-control" placeholder="用户名" required=""  ref="username"/>
            <input type="password" className="form-control" placeholder="密码" required="" ref="password"/>
            <button className="btn btn-lg btn-primary btn-block" type="submit">登录</button>
          </form>

    );
  }

   handleSubmit(e) {
        e.preventDefault();
        const username=this.refs.username.value;
        const password=this.refs.password.value;
        // console.log(username,password)
        const {login}=this.props;
        login(username,password).then(this.gotoLoginAfter).catch(this.showError)
    }

    gotoLoginAfter(){
        const routeSuccess=this.props.routeSuccess||'login_after';
        PubSub.publish('route',routeSuccess);
    }
    showError(){
        PubSub.publish('msg.error','登陆错误');
    }
}

//接受pubsub的部分:
<script src="http://cdn.bootcss.com/pubsub-js/1.5.4/pubsub.min.js"></script>
<link href="http://cdn.bootcss.com/toastr.js/latest/css/toastr.min.css" rel="stylesheet">
<script src="http://cdn.bootcss.com/toastr.js/latest/toastr.min.js"></script>
<script>
    PubSub.subscribe( 'route', (msg,route)=>{
    toastr.info("goto:"+route);//方便调试路由跳转,正式代码中不出现
    location.href=route;//路由跳转
    }); 
    PubSub.subscribe( 'msg.error', (msg,str)=>{toastr.error(str)} ); //toast,全局消息。正式代码中也有
</script>

相关文章

  • 理想的web组件模式

    面向接口编程 写功能之前,先写接口。前端和后端之间的接口:RESTful API后端用express的情况下,这个...

  • Web 渲染(1)

    web 应用的架构 渲染和加载 (Rendering 和 loading) 数据/状态管理 组件模式 路由和转换 ...

  • partridge自动化测试平台

    1. 背景 oss软件整体采用前后端分离的模式,前端采用JQuery自研组件,包含常用web组件。后端采用spri...

  • Part II 3.从Web应用开始(Getting Start

    Web应用 在Java EE平台中,**Web组件**为Web服务器提供动态扩充的能力。Web组件可以是Java ...

  • 10.Web Components(web组件)

    React版本:15.4.2**翻译:xiyoki ** React和Web组件用于解决不同的问题。Web组件为可...

  • 第十三次早课

    第十三次早课 1、hadoop三大组件 2、我们选择了哪种模式安装 3、hdfs yarn的web界面默认端口分别...

  • React进阶笔记13(Web Components)

    React 和 web组件 被用以解决不同问题。web组件为可重用的组件提供了强大的封装能力。React则是保持D...

  • WebService

    ** 什么是Web Services?** Web Services 是应用程序组件 Web Services 使...

  • React-2:组件、

    组件 web组件就是对web中的数据、结构、方法等进行封装,复用,与JavaScript中功能函数封装类似...

  • 4-React 组件之状态与通信

    React.js [TOC] 组件 web 组件就是对 web 中的数据、结构、方法等进行封装,复用,与 Java...

网友评论

      本文标题:理想的web组件模式

      本文链接:https://www.haomeiwen.com/subject/ndrnvttx.html