简体   繁体   English

react.js:如何加载本地react.js而不是使用在线的

[英]react.js: how to load local react.js instead of using online ones

So I'm new to react.js and have been following the tutorial created by facebook. 因此,我是React.js的新手,并且一直在关注facebook创建的教程。 ( https://facebook.github.io/react/docs/tutorial.html ; this one). https://facebook.github.io/react/docs/tutorial.html ;这一个)。

Now I finish the tutorial I want to expand a little bit on top of it. 现在,我完成了本教程,我想在此基础上再扩展一点。 However, I notice that in the tutorial, in index.html, it is using online locations for loading react and other modules. 但是,我注意到在本教程的index.html中,它使用在线位置来加载react和其他模块。 One example is like this: 一个例子是这样的:

How to change the src from online to local? 如何将src从在线更改为本地? I've download react through npm. 我已经通过npm下载了react。 All files are in a folder called react2. 所有文件都在名为react2的文件夹中。 index.html is in react2/public, server.js is in react2 root folder; index.html在react2 / public中,server.js在react2根文件夹中; while react is in react2/node_modules/react/react.js. 而react是在react2 / node_modules / react / react.js中。 I've tried to change src into "../node_modules/react/react.js" and "/home/.../react2/..." and tried to add file:/// in the front, but nothing worked. 我试图将src更改为“ ../node_modules/react/react.js”和“ / home /.../ react2 / ...”,并尝试在前面添加file:///,但是什么也没有工作。

btw, this is my first question on stackoverflow and I'm not really familiar with the rules. 顺便说一句,这是我关于stackoverflow的第一个问题,我对规则并不真正熟悉。 If I did something wrong in posting please tell me. 如果我在发布中做错了什么,请告诉我。

Thanks a lot! 非常感谢!

If you want to import react in a html file through the src attribute of the script tag, you should download the files directly from here react-15.2.0.min.js and react-dom-15.2.0.min.js and save them into a lib folder, or wherever you want. 如果要通过script标记的src属性将act导入html文件,则应从此处直接下载文件react-15.2.0.min.jsreact-dom-15.2.0.min.js并保存将它们保存到lib文件夹中,或放置在任何位置。

If you download React from npm you have to import react like any other npm package and generate a bundle that you can import in your html file with a bundler like browserify or webpack. 如果您从npm下载React,则必须像其他npm软件包一样导入react并生成一个包,您可以使用bundler(如browserify或webpack)将其导入html文件。 You can use the react and react-dom packages. 您可以使用react和react-dom软件包。 After installing it using npm install --save react react-dom, you can use: 使用npm install --save react react-dom安装它之后,可以使用:

var React = require('react');
var ReactDOM = require('react-dom');
ReactDOM.render(<App />, ...);

Speaking this way is hard to detect what problem is. 用这种方式说话很难发现问题所在。 I'm almost sure is related to call React in a wrong path. 我几乎确定与在错误的路径中调用React有关。

You can open your Developer Tools inside your preferred browser, to check which source your app is calling React. 您可以在首选浏览器中打开开发人员工具,以检查您的应用正在调用React的源。 After opening it, go to the console tab and there you can check for every missing file. 打开它后,转到console选项卡,您可以在其中检查每个丢失的文件。

Also, make sure you're running React on a server. 另外,请确保您正在服务器上运行React。 Which is required for most actions you'll use. 您将要使用的大多数操作都需要执行此操作。 If React is being called properly, but isn't working, maybe it could be the problem. 如果React被正确调用,但无法正常工作,则可能是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM