本文介绍了在ejs中使用React?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用React状态构建的前端,该状态旨在根据用户操作进行调整.但是,我的前端React也打算显示并允许操纵我的服务器端数据.目前,我的视图引擎是EJS,并且正在使用它来显示数据.举一个广泛的例子:

I have a front-end that is built using React states that is meant to adapt based on user action. However, my front-end React is also meant to show and allow manipulation of my server-side data. Currently my view engine is EJS, and I am using it to display data. As a broad example:

  return (<div class="col-md-6 col-sm-6 col-xs-7">
    <ul>
      <li><span class="point">Name:</span> <%= user.profile.name %> </li>
      <li><span class="point">Email:</span> <%= user.email %> </li>
    </ul>
  </div>);

我已经确定我不能将这些ejs <%= 标记与React混合使用.这使得处理数据成为一个挑战.除非我在jQuery中重做UI,否则我不确定如何继续.

I have established that I cannot mix these ejs <%= tags with React. This makes manipulating the data a challenge. Unless I redo my UI in jQuery, I'm not sure how to proceed.

我查看了此React文档以传递数据,但是经过测试,结果不允许我进行跨域调用,并且我的MongoDB存储在MongoLab上.因此,我被降级为使用EJS来调用我的数据.

I have looked at this React documentation for passing data, but upon testing it the result does not allow me to make cross-origin calls, and my MongoDB is stored on MongoLab. Thus, I am relegated to using EJS to call my data.

由于将React与EJS一起使用的局限性,令我困惑的是,要使用服务器端数据来实现UI这样的UI工具,必须采用什么解决方案.

With the restrictions of using React with EJS, I am puzzled over what solutions I have to implement a UI tool like React with server-side data.

推荐答案

快递:

res.render('view', {user: myUser});

在应用程序的js捆绑包之前的EJS中:

In EJS before the app's js bundle:

<script type='text/javascript'>
  var userFromServer =<%-JSON.stringify(user)%>
</script>

在您的反应代码中使用 userFromServer .

Use userFromServer in your react code.

这篇关于在ejs中使用React?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:03