- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有包含以下页面的基本 React 应用程序:主页、个人资料、联系方式和体验。我已经为每个页面设置了路由,但是当我转到主页以外的其他页面时,它会呈现,但是当我刷新页面时,页面不会加载。
我注意到如果我在页面名称前添加 #
,例如页面呈现的 http://localhost:1234/#/profile
。所以我很困惑为什么我需要我不想要的 #
而我正在使用 react-router-dom
,所以不需要 #
。
我将 historyApiFallback
添加到我的 webpack.config 中,但这不起作用。谁能帮我这个?我是新手,我想尽可能多地学习。我们将不胜感激您的帮助!
应用程序.jsx
import React, { Component } from "react";
import Navbar from "./components/navbar";
import Intro from "./components/introPage";
import Experience from "./components/experiencePage";
import Profile from "./components/profilePage";
import Contact from "./components/contactPage";
import { BrowserRouter as Router, Link, Route } from 'react-router-dom';
class App extends Component {
render(){
return (
<Router>
<div className="pageSections">
<Navbar />
<div className="navContent">
<Route exact path="/" component={Intro}/>
<Route path="/experience" component={Experience}/>
<Route path="/profile" component={Profile}/>
<Route path="/contact" component={Contact}/>
</div>
</div>
</Router>
);
}
}
export default App;
导航栏.jsx
import React, { Component } from "react";
import { Link } from "react-router-dom";
class Navbar extends Component {
render(){
return (
<div className="navFrame">
<Link to="/">
<div className="topNav"><div className="navBar"><h3>Marteen</h3></div></div>
</Link>
<Link to="/profile">
<div className="rightNav"><div className="navBar"><h3>Profile</h3></div></div>
</Link>
<Link to="/experience">
<div className="bottomNav"><div className="navBar"><h3>Experience</h3></div></div>
</Link>
<Link to="/contact">
<div className="leftNav"><div className="navBar"><h3>Contact</h3></div></div>
</Link>
</div>
);
}
}
export default Navbar;
webpack.config.js
const webpack = require('webpack');
const config = {
entry: __dirname + '/js/index.jsx',
output: {
path: __dirname + '/dist',
filename: 'bundle.js',
},
resolve: {
extensions: ['.js', '.jsx', '.css']
},
module: {
rules: [
{
test: /\.jsx?/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
test: /\.scss?/,
loader: 'style-loader!css-loader!sass-loader'
}
]
},
devServer: {
historyApiFallback: true,
contentBase: './',
hot: true
}
};
module.exports = config;
包.json
{
"main": "index.js",
"scripts": {
"build": "webpack -p --progress --config webpack.config.js",
"dev-build": "webpack --progress -d --config webpack.config.js",
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack --progress -d --config webpack.config.js --watch",
"start": "npm run open",
"open": "concurrently \"http-server -a localhost -p 1234\" \"open http://localhost:1234\""
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.4",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"concurrently": "^3.6.1",
"css-loader": "^0.28.11",
"http-server": "^0.11.1",
"node-sass": "^4.7.2",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"sass-loader": "^6.0.7",
"style-loader": "^0.20.3",
"webpack": "^4.1.1",
"webpack-cli": "^2.0.12"
},
"dependencies": {
"npm": "^5.10.0"
}
}
更新
包.json
{
"name": "fullstack_profile",
"version": "1.0.0",
"description": "fullstack profile with flask and react",
"main": "index.js",
"scripts": {
"build": "webpack -p --progress --config webpack.config.js",
"dev-build": "webpack --progress -d --config webpack.config.js",
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack-dev-server --hot --progress --mode development",
"start": "npm run open",
"open": "concurrently \"http-server -a localhost -p 1234\" \"open http://localhost:1234\""
},
"repository": {
"type": "git",
"url": "git+https://github.com/medev21/fullstack_profile.git"
},
"author": "martin",
"babel": {
"presets": [
"es2015",
"react"
]
},
"license": "ISC",
"bugs": {
"url": "https://github.com/medev21/fullstack_profile/issues"
},
"homepage": "https://github.com/medev21/fullstack_profile#readme",
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.4",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"concurrently": "^3.6.1",
"css-loader": "^0.28.11",
"http-server": "^0.11.1",
"node-sass": "^4.7.2",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"sass-loader": "^6.0.7",
"style-loader": "^0.20.3",
"webpack": "^4.1.1",
"webpack-cli": "^2.0.12",
"webpack-dev-server": "^3.1.5"
},
"dependencies": {
"npm": "^5.10.0"
}
}
webpack.config
const webpack = require('webpack');
const config = {
entry: __dirname + '/js/index.jsx',
output: {
path: __dirname + '/dist',
filename: 'bundle.js',
},
resolve: {
extensions: ['.js', '.jsx', '.css']
},
module: {
rules: [
{
test: /\.jsx?/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
test: /\.scss?/,
loader: 'style-loader!css-loader!sass-loader'
}
]
},
devServer: {
contentBase: __dirname + '/dist',
compress: false,
port: 1234,
historyApiFallback: {
index: 'index.html'
}
}
};
module.exports = config;
最佳答案
为什么http://localhost:1234/#/profile的原因有效是因为# 不重新加载页面。它的行为与 HTML 中的 anchor 标记相同,您停留在同一页面上但滚动到它的特定部分。例如,下面将您滚动到页面的投资组合部分。
<a href="www.page.com/#portfolio">Portfolio</a>
如果省略#,则不同。这会告诉您的服务器重新加载页面并访问该位置。在你的情况下 http://localhost:1234/profile服务器未知,因为您尚未在服务器端指定它。在这些情况下,您需要创建路由或在找不到路由时代理请求。
当您使用作为客户端路由器的 react-router 时,服务器需要提供相同的文件,因此您应该使用代理选项。
使用 http-server
时 the docs假设您可以添加 -P 或 --proxy 标志来指定它。
-P or --proxy Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com
在您的情况下,请更新您的 package.json 中的以下内容。
"open": "concurrently \"http-server -a localhost -p 1234 -P http://localhost:1234\" \"open http://localhost:1234\""
另一种方法是使用 webpack-dev-server
进行开发。它将大大改善您的开发人员体验,因为它支持组件更改时的热重新加载。
使用 npm install --save-dev webpack-dev-server
安装它
将以下内容添加到您的 webpack 配置中。
devServer: {
contentBase: __dirname + '/dist',
compress: false,
port: 1234,
historyApiFallback: {
index: 'index.html' // assuming this is your entry point file that loads your bundle.
}
},
然后在您的 package.json 中为其添加一个脚本并使用 npm run watch
运行它
"watch": "webpack-dev-server --hot --progress --mode development",
注意:确保您的 dist 文件夹中存在 index.html
。否则,您将遇到问题。
我创建了一个 basic project on GitHub所以你有一个工作示例。
关于javascript - 页面不会在刷新时加载 - react-router-dom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51566221/
我有一个已发布的库,其中包含一个在其模板中使用 [routerLink] 的组件。在我的应用程序中安装库后,我收到错误 NullInjectorError:R3InjectorError(AppMod
/~/@ReMix-Run/Router/dist/router.cjs.js模块分析失败:/HOME/sharib/Desktop/Full Stack Developer Tutorial/MER
在 vuejs 2 中,我能够通过返回渲染 html 标签直接在路由器中渲染。我正在尝试使用 vue-router 4 在 vue3 中做同样的事情,但它似乎不起作用: { path:
在我的应用程序中,我使用 vue-router。当我使用 router.map({}) 将路由传递给路由器时,路由可以工作,但当我在构造函数中传递它们时,路由就不起作用。知道这是为什么吗? // wo
我正在 react 应用程序中实现路由。我对react-router 4.0相对于react-router-redux的优势感到困惑。我也对react-router-dom感到困惑。 react-ro
我有以下两条路线:/items和/items/buy。 每个路线在一个 View 中对应一个选项卡。两条路线均使用精确的 Prop 进行渲染,但导航至/items/buy时,仍将两个选项卡标记为事件选
我有一个带有 url/product/0 的新产品页面。当使用按钮保存产品时,我想显示“已成功保存”消息并将 url 更改为/product/1024 而无需实际导航到该页面,因为这会导致对页面上的产
我对此有些困惑。我定义了一条名为classes/:id的路线。在应用程序中导航到该路线时,将调用componentDidMount()。但是,当重新加载页面或复制并粘贴URL时,页面将完全加载,但根本
假设我有以下2条路线: ... ... 现在,当我尝试按路线/news时,会触发带有Home参数的param1的根路线... 我认为解决方案是在像/?param1这
我正在将 Vue Router 与 Vue 3 一起使用,并尝试添加一个包罗万象的路由以在用户尝试访问无效 URL 时重定向用户。当我尝试使用通配符 (*) 时,我将以下错误记录到控制台: Uncau
我在我的项目中使用react-router-dom 4.0.0-beta.6。我有如下代码: 我想在 HomePage 组件中获取查询参数。我找到了 location.search 参数,它看起来像
我正在使用 Vue 路由器。在我的代码中,我曾经有: 这有效,并且页面数据被传递到组件。但是如何使用路由器 View 执行相同的操作呢?这似乎不起作用: js: var vm = new Vue(
我目前正在一个项目中使用react-router-redux,除了我不知道如何访问react-router路由器之外,一切都工作正常。 @connect((state, props) => {
当使用链接时,路由器按预期工作,尽管我收到警告 [history] pushState is deprecated;使用推送代替。 使用react-router-redux中的routeActio
我想创建一个多页 Meteor 网络和移动应用程序。因此,我正在寻找路由服务。 来自 research ,我收集了Iron Router就是我要找的。但是很多教程也提到了Meteor.Router I
我正在使用 React Router(版本 4.0.0)和 Router组件有一个名为“history”的必需 Prop 。 我知道在以前版本的react-router中你可以导入browserHis
我正在使用 氟罗包用于导航。 Fluro 和 Flutter 正在使用“路由器”。所以他们的类(class)合并了。我怎么能解决这个问题? lib/routers/routers.dart:2:1:
自从我更新了 Vue.js 应用程序的节点包后,我在浏览器控制台中收到以下警告: [vue-router] 's tag prop is deprecated and has beenremoved
我正在尝试在我的应用程序中将 react-router 更新到 v2.6 并将 react-router-relay 更新到 v0.7,但我正在努力按照变更日志来解决所有重大变更。我想我解决了所有的变
router-view 和 router-link 组件无法在我的应用中解析不知道是什么问题 附上应用代码它是一个主页应用程序,带有类似 navbar 的东西,用于路由到 vue 组件,如 state
我是一名优秀的程序员,十分优秀!