React+BootStrap+ASP.NET MVC实现自适应和组件的复用

前端之家收集整理的这篇文章主要介绍了React+BootStrap+ASP.NET MVC实现自适应和组件的复用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、前端采用bootstrap3进行架构

2、后端采用asp.net mvc进行开发

开发工具vs2010 mvc4需要安装sp1的补丁。

3、js组件的封装采用react

  1. 新建mvc项目
    BootStrapReactAndMVC。在Views\Shared\新建_Layout.cshtml文件。将bootstrap的引用进行添加
  2. 新建HomeController和ReportController两个Controller对象。
  3. 新建renHangcpu.jsx文件文件内容如下
var cpuWatch = React.createClass({
  render: function() {
    return (<div>
        <h2 className="sub-header">cpu监控</h2>
        <div className="row placeholders">
          <div className="col-xs-6 col-sm-3 placeholder">
            <img data-src="holder.js/200x200/auto/sky" className="img-responsive" alt="Generic placeholder thumbnail"/>
            <h4>cpu1</h4>
            <span class="text-muted">使用率10%</span>
          </div>
          <div className="col-xs-6 col-sm-3 placeholder">
            <img data-src="holder.js/200x200/auto/vine" className="img-responsive" alt="Generic placeholder thumbnail"/>
            <h4>cpu2</h4>
            <span class="text-muted">使用率10%</span>
          </div>
          <div className="col-xs-6 col-sm-3 placeholder">
            <img data-src="holder.js/200x200/auto/sky" className="img-responsive" alt="Generic placeholder thumbnail"/>
            <h4>cpu3</h4>
            <span class="text-muted">使用率10%</span>
          </div>
          <div className="col-xs-6 col-sm-3 placeholder">
            <img data-src="holder.js/200x200/auto/vine" className="img-responsive" alt="Generic placeholder thumbnail"/>
            <h4>cpu4</h4>
            <span class="text-muted">使用率20%</span>
          </div>
        </div>
      </div>
    );
  }
})

新建renhangyingyanjsx.jsx文件文件内容如下

var YingPanWatch = React.createClass({
  render: function () {
    return (<div><h2 className="sub-header">硬盘吞吐量</h2>
      <div className="table-responsive">
        <table className="table table-striped">
          <thead>
          <tr>
            <th>#</th>
            <th>磁盘1</th>
            <th>磁盘2</th>
            <th>磁盘3</th>
            <th>磁盘4</th>
          </tr>
          </thead>
          <tbody>
          <tr>
            <td>1,001</td>
            <td>100</td>
            <td>100</td>
            <td>100</td>
            <td>100</td>
          </tr>
          <tr>
            <td>1,001</td>
            <td>100</td>
            <td>100</td>
            <td>100</td>
            <td>100</td>
          </tr>
          </tbody>
        </table>
      </div>
    </div>)
  }
})

sx不能被直接引用。我们要用ES的编译器进行编译。

http://babeljs.cn/repl/ 在这个网址上进行编译。

编译后的文件如下

renHangcpu1.js 可以直接引用

@H_301_461@"use strict";

var cpuWatch = React.createClass({
  displayName: @H_301_461@"cpuWatch",render: function render() {
    return React.createElement(
      @H_301_461@"div",null,React.createElement(
        @H_301_461@"h2",{ className: @H_301_461@"sub-header" },@H_301_461@"cpu监控"
      ),React.createElement(
        @H_301_461@"div",{ className: @H_301_461@"row placeholders" },React.createElement(
          @H_301_461@"div",{ className: @H_301_461@"col-xs-6 col-sm-3 placeholder" },React.createElement(@H_301_461@"img",{ @H_301_461@"data-src": @H_301_461@"holder.js/200x200/auto/sky",className: @H_301_461@"img-responsive",alt: @H_301_461@"Generic placeholder thumbnail" }),React.createElement(
            @H_301_461@"h4",@H_301_461@"cpu1"
          ),React.createElement(
            @H_301_461@"span",{ @H_301_461@"class": @H_301_461@"text-muted" },@H_301_461@"使用率10%"
          )
        ),{ @H_301_461@"data-src": @H_301_461@"holder.js/200x200/auto/vine",@H_301_461@"cpu2"
          ),@H_301_461@"cpu3"
          ),@H_301_461@"cpu4"
          ),@H_301_461@"使用率20%"
          )
        )
      )
    );
  }
});

renhangyingpanjs.js 可以直接被引用

"use strict";

var YingPanWatch = React.createClass({
  displayName: @H_301_461@"YingPanWatch",render: function render() {
    return React.createElement(
      @H_301_461@"div",null,React.createElement(
        @H_301_461@"h2",@H_301_461@"硬盘吞吐量"
      ),React.createElement(
        @H_301_461@"div",{ className: @H_301_461@"table-responsive" },React.createElement(
          @H_301_461@"table",{ className: @H_301_461@"table table-striped" },React.createElement(
            @H_301_461@"thead",React.createElement(
              @H_301_461@"tr",React.createElement(
                @H_301_461@"th",@H_301_461@"#"
              ),@H_301_461@"磁盘1"
              ),@H_301_461@"磁盘2"
              ),@H_301_461@"磁盘3"
              ),@H_301_461@"磁盘4"
              )
            )
          ),React.createElement(
            @H_301_461@"tbody",React.createElement(
                @H_301_461@"td",@H_301_461@"1,001"
              ),@H_301_461@"100"
              ),@H_301_461@"100"
              )
            ),@H_301_461@"100"
              )
            )
          )
        )
      )
    );
  }
});

4、然后在下面的view Home/Index上我们添加如下的代码

@{
    ViewBag.Title = @H_301_461@"欢迎使用";
      ViewBag.WhichPage=@H_301_461@"Home";
    Layout = @H_301_461@"~/Views/Shared/_Layout.cshtml";
 }


  @section Scripts {
    <script type=@H_301_461@"text/babel">
      ReactDOM.render(
    <cpuWatch/>,document.getElementById('divcpu')
    );

      ReactDOM.render(
        <YingPanWatch/>,document.getElementById('divYingpan')
      );

  </script>
  }

   <div class=@H_301_461@"col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main" >
          <div id=@H_301_461@"divcpu"></div>
          <div id=@H_301_461@"divYingpan"></div>
          </div>

在Report的Index上添加如下代码

@{
    ViewBag.Title = @H_301_461@"欢迎使用";
    Layout = @H_301_461@"~/Views/Shared/_Layout.cshtml";
    ViewBag.WhichPage=@H_301_461@"Report";
 }


  @section Scripts {
    <script type=@H_301_461@"text/babel">
      ReactDOM.render(
    <cpuWatch/>,document.getElementById('divYingpan')
      );

  </script>
  }

   <div class=@H_301_461@"col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main" >

          <div id=@H_301_461@"divYingpan"></div>
           <div id=@H_301_461@"divcpu"></div>
          </div>

运行结果如图:



猜你在找的React相关文章