get backend var from php into reactjs via webpack

旧时模样 提交于 2019-12-12 03:54:20

问题


How can I get a php backend var into my reactjs (using react-router) via webpack.

I want to do something like:

<?php 
  if ($us->inGroup('Admin')) { ?>
    <script>
      var groups = true
    </script>
<?php 
  }
?>

Then use that var groups inside my reactjs app via webpack (inside the bundle.js)

Here, where my reactjs app is -> <script type="application/javascript" src="/bundle.js"></script>

Thank you.


回答1:


You don't need Webpack to do this. You just need to make sure that the groups variable is in the global scope. In your .php file, set the groups value by using a ternary statement to echo true or false based on the value return from the inGroup call.

<script>
  var groups = <?php echo $us->inGroup('Admin') ? 'true' : 'false'?>;
</script>


来源:https://stackoverflow.com/questions/41449909/get-backend-var-from-php-into-reactjs-via-webpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!