当我点击包含网址products/new
的链接时,我可以访问新的产品页面并且工作正常,但如果我刷新页面则返回Uncaught SyntaxError: Unexpected token <
。
我该如何解决这个问题?
import React from 'react';
class ProductNew extends React.Component {
constructor(props) {
super(props);
}
render(){
return (
<div>
<div><h1>ProductNew</h1></div>
</div>
);
}
}
module.exports = ProductNew;
import React from 'react';
import { Link } from 'react-router';
class Products extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<div><h1>Products</h1></div>
<Link to="/products/new">new</Link>
</div>
);
}
}
module.exports = Products;
render((
<Router history={createBrowserHistory()}>
<Route path="/" component={Layout}>
<IndexRoute component={Home} />
<Route path="/products" component={Products}/>
<Route path="/products/new" component={ProductNew}/>
</Route>
</Router>
), document.getElementById('app'));
答案 0 :(得分:2)
我有类似的问题,我的App.js路径是相对的。 因此,当我从主路径加载应用程序页面时,一切运行良好,但当我尝试从您的“/ product / new”链接加载时,我收到了类似您的错误。当我提出绝对路径时,它运作良好。
我使用webpack捆绑文件和webpack-dev-server来运行开发环境,当你使用webpack-dev-server hot loader(默认情况下)时没有物理文件,这是很容易的事情落入。
编辑: this guy's question有2个更新,与您有同样的问题,并且有比我给你更好的答案/信息。
答案 1 :(得分:0)
当你为组件使用嵌套路径时,如 http://127.0.0.1:8000/dashboard/myprojects 那么你必须在主索引文件中为你的 css 和 js 添加绝对路径..例如在 laravel index.php.blade 文件中添加绝对路径像这样: <'script src="{{ mix('js/app.js') }}"> '脚本>