问题
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