【发布时间】:2018-02-14 09:19:25
【问题描述】:
这似乎很容易,但我完全糊涂了。
我已经开发了带有组件的反应应用程序。现在在我的 index.html 我有工作示例:
<div class="container">
<div id="app-content">
<!-- React will render in div#app-content, works fine! -->
<script src="build/main.bundle.js"></script> </div> </div>
项目目录:
我想从浏览器访问组件反应组件应用程序 java 脚本,以在现有项目中实现此应用程序,例如:
<div class="container">
<div id="component">
<script>
// how can i call App component to render
// in div#components from here
</script>
</div>
</div>
文件 build/main.bundle.js 是使用webpack 生成的, 我有以下文件: webpack.config.js:
var webpack = require("webpack"); module.exports = { /* This is the default setting for webpack */ target: "web", /* Generate source-maps for browser side debugging */ devtool: "source-map", /* Entry file to start building from. This is where you will want to start * your project. If you wanted to build multiple entry points you could * list them below. */ entry: { main: "./src/main" }, /* Defines where to output the final built files. The [name] definition * is based off of the entry point's name. This example will generate * a main.bundle.js in the public/build directory. */ output: { path: "./public/build", filename: "[name].bundle.js" }, resolve: { /* Defines where it can load modules from. Since we've installed our * JS dependencies through npm, it can look in the node_modules * directory. If you use bower, you can add 'bower_components' * to the list. */ modulesDirectories: ['node_modules'], /* List extensions to load in require() statements. The '' entry * is needed to allow you to require src/main.jsx as require('src/main.jsx') */ extensions: ['', '.js', '.jsx'] }, /* Defines what modules to use */ module: { /* Loaders are how webpack compiles and builds the JSX extensions */ loaders: [ /* Any file with a .jsx extension will go through the jsx-loader */ { test: /\.jsx$/, loader: "jsx-loader?harmony" } //add loader // { test: /\.jsx$/, loader: ""} ] }, //uncoment to use compression // plugins: [ // new webpack.optimize.UglifyJsPlugin({ // compress: { // warnings: false // } // }), // ] };
main.jsx:
var React = require('react'); var App = require('./components/App'); if(typeof document !== "undefined") { React.render(<App />, document.getElementById("app-content")); }
App.jsx:
var React = require('react'); var LineChart = require('./LineChart'); var HelloWorld = require('./HelloWorld'); var chartOptions = { responsive: true }; var dataBar = { labels: ["January", "February", "March", "April", "May", "June", "July"], datasets: [ { label: "My First dataset", fillColor: "rgba(220,220,220,0.5)", strokeColor: "rgba(220,220,220,0.8)", highlightFill: "rgba(220,220,220,0.75)", highlightStroke: "rgba(220,220,220,1)", data: [65, 59, 80, 81, 56, 55, 40] }, { label: "My Second dataset", fillColor: "rgba(151,187,205,0.5)", strokeColor: "rgba(151,187,205,0.8)", highlightFill: "rgba(151,187,205,0.75)", highlightStroke: "rgba(151,187,205,1)", data: [28, 48, 40, 19, 86, 27, 90] } ] }; var dataLine = { labels: ["Viens", "Divi", "Tris", "Cetri", "Pieci", "Sesi", "Septini"], datasets: [ { label: "My First dataset", data: [65, 59, 80, 81, 56, 55, 40, 88] }, { label: "My Second dataset", data: [28, 48, 40, 19, 86, 27, 90, 98] } ] }; var dataLine2 = { labels: ["January", "February", "March", "April", "May", "June", "July"], datasets: [ { label: "My First dataset", data: [11, 15, 20, 25, 30, 40,70] }, { label: "My Second dataset", data: [80, 50, 49, 10, 20, 35, 55, 44] }, { label: "My Third dataset", data: [102, 50, 80, 65, 25,80, 90] }, { label: "My 4 dataset", data: [150, 50, 50, 50, 50, 70, 89] }, { label: "My 5 dataset", data: [80, 20, 50, 78, 65, 45, 100] }, { label: "My 6 dataset", data: [100, 90, 80, 70, 60, 50, 40] }, { label: "My 7 dataset", data: [55,66,77,88,99,100,44] }, ] }; var data = { startDate: "13/01/2015", endDate: "30/04/2015", title: 'LineChartDemo', chartData: dataLine }; var data2 = { startDate: "13/01/2015", endDate: "30/04/2015", title: 'LineChartDemo', chartData: dataLine2 }; var App = React.createClass({ render: function(){ return ( <div id="app"> <HelloWorld name="Rolands Usāns" /> <LineChart data={data} imageExport={['png, jpeg']}/> <LineChart data={data2} imageExport={['png, jpeg']}/> </div> ); } }); module.exports = App;
【问题讨论】:
-
我不明白你在做什么。为什么要在 DOM 中间执行脚本?
-
@wiredpraire 现在我有基于 MVC 原则的项目(我有框架 Yii 1.1.15),所以我想在现有视图中部分实现 React 组件。
-
只需使用
React.render并定位特定元素。您不需要使用内联脚本来完成这项工作。 -
当我从 index.html
<div class="container"> <div id="component"> <script> // how can i call App component to render // in div#components from here </script> </div> </div>调用 React.render 我得到React is undefined,我已经包含了 main.bundle.js 生成通过 webpack -
你确定这个包包含 React 吗?检查文件。看起来要么没有,要么你在下载之前尝试使用 React。
标签: javascript reactjs