[英]dynamic routing with reactjs not working
i am new to react.i was trying dynamic routing but it is not working . 我是react.i的新手,我正在尝试动态路由,但无法正常工作。
i was creating dynamic routing with react-router .i created on route named "/edit" and added that :id after it so i can dynamically access its value ..but whenever i go to http://localhost:8080/edit/22 it shows me this error 我正在用react-router创建动态路由。我在名为“ / edit”的路由上创建并在其后添加了:id,以便我可以动态访问其值..但是无论何时我去http:// localhost:8080 / edit / 22它告诉我这个错误
GET http://localhost:8080/edit/bundle.js net::ERR_ABORTED
Refused to execute script from 'http://localhost:8080/edit/bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
and also doesn't render the page. 并且也不会呈现页面。 see the screenshot.
参见屏幕截图。
part of code 代码的一部分
const EditExpenseDashboardPage = (props) => {
console.log(props)
return (
<div>
<h3>EditExpense Dashboard page</h3>
<Link to="/add">Add</Link>
<Link to="/help">help</Link>
</div>
)}
const routes = (
<BrowserRouter>
<div>
<Header/>
<Switch>
<Route path="/" component={ExpenseDashboardPage} exact={true} />
<Route path="/add" component={AddExpenseDashboardPage} />
<Route path="/edit/:id" component={EditExpenseDashboardPage} />
<Route path="/help" component={HelpExpenseDashboardPage} />
<Route component={ErrorWala} />
</Switch>
</div>
</BrowserRouter>
)
thanks for help. 感谢帮助。
[] https://i.stack.imgur.com/wWOzB.png [] https://i.stack.imgur.com/wWOzB.png
The error occurs because the script is trying to load bundle.js
from a path based on your route. 发生错误是因为脚本试图根据您的路由从路径加载
bundle.js
。 ( http://localhost:8080/edit/bundle.js ) There are two ways to go about preventing this depending on your setup. ( http:// localhost:8080 / edit / bundle.js )有两种方法可以防止这种情况,具体取决于您的设置。
If you are using html-webpack-plugin
to generate the base html file, then you have to provide 如果您使用
html-webpack-plugin
生成基本html文件,则必须提供
output: {
filename: "[hash].bundle.js",
path: commonPaths.outputPath,
publicPath: "/" // Notice this line
},
publicPath
parameter as '/' in webpack.config.js
. publicPath
参数作为在'/' webpack.config.js
。 This will make all output assets refer to '/' as the root instead of depending on the route. 这将使所有输出资产都将“ /”作为根,而不是取决于路由。
If you are just using a pre made html
file, changing the script src to use /bundle.js
will suffice. 如果您仅使用预制的
html
文件,则将脚本src更改为使用/bundle.js
就足够了。 You still have to provide the publicPath
for other assets imported into javascript. 您仍然必须为导入JavaScript的其他资产提供
publicPath
。
https://webpack.js.org/guides/public-path/ https://webpack.js.org/guides/public-path/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.