gpt4 book ai didi

javascript - react 路由器|组件未渲染

转载 作者:行者123 更新时间:2023-12-03 02:48:53 26 4
gpt4 key购买 nike

我有一个具有多条路线的 SPA。我正在尝试使用 Webpack 2 和 React Router 4 基于路由实现代码分割。我能够根据路由创建不同的 block 。例如,对于主页,我有一个 vendor.jsmain.jshome.js。所有三个文件均已成功加载,但我没有看到任何输出。 null 正在渲染。下面是 React 开发者工具和使用的代码的屏幕截图。我可以知道我在这里缺少什么吗?

enter image description here

应用程序组件/App.js

import React, { Component } from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import NotificationContainer from '../containers/NotificationContainer';

class App extends Component {
constructor(props) {
super(props);
}

render() {
return (
<div>
<NotificationContainer />
<BrowserRouter>
<Switch>
<Route
exact
path="/"
getComponent={(nextState, callback) => {
// import('./homepage/index').then(module => callback(null, module.default));
require.ensure(
[],
require => {
callback(null, require('./homepage/index').default);
},
'home'
);
}}
/>
<Route
path="/login"
getComponent={(nextState, callback) => {
require.ensure(
['../containers/LoginContainer'],
require => {
require('../containers/LoginContainer');
},
'login'
);
}}
/>
<Route
path="/forgetPassword"
getComponent={(nextState, callback) => {
require.ensure(
['../containers/ForgetPasswordContainer'],
require => {
require('../containers/ForgetPasswordContainer');
},
'forgetPassword'
);
}}
/>
<Route
path="/generateNewPassword"
getComponent={(nextState, callback) => {
require.ensure(
['../containers/GenerateNewPasswordContainer'],
require => {
require('../containers/GenerateNewPasswordContainer');
},
'generateNewPassword'
);
}}
/>
<Route
path="/signup"
getComponent={(nextState, callback) => {
require.ensure(
['../containers/SignupContainer'],
require => {
require('../containers/SignupContainer');
},
'signup'
);
}}
/>
<Route
path="/contact"
getComponent={(nextState, callback) => {
require.ensure(
['./Contact'],
require => {
require('./Contact');
},
'contact'
);
}}
/>
<Route
path="/tech"
getComponent={(nextState, callback) => {
require.ensure(
['./Tech'],
require => {
require('./Tech');
},
'tech'
);
}}
/>
<Route
path="/error"
getComponent={(nextState, callback) => {
require.ensure(
['./Error'],
require => {
require('./Error');
},
'error'
);
}}
/>
<Route
path="/user/dashboard"
getComponent={(nextState, callback) => {
require.ensure(
['../containers/DashBoardContainer'],
require => {
require('../containers/DashBoardContainer');
},
'dashboard'
);
}}
/>
<Route
path="/movie/:movieId"
getComponent={(nextState, callback) => {
require.ensure(
['../containers/MovieContainer'],
require => {
require('../containers/MovieContainer');
},
'movieContainer'
);
}}
/>
<Route
getComponent={(nextState, callback) => {
require.ensure(
['./Error'],
require => {
require('./Error');
},
'error'
);
}}
/>
</Switch>
</BrowserRouter>
</div>
);
}
}

export default App;

Webpack 配置

const webpack = require('webpack');
var BundleTracker = require('webpack-bundle-tracker');
var BundleAnalyzer = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

module.exports = {
entry: './src/index.js',
output: {
path: __dirname + '/public/assets/js',
filename: '[name].js',
chunkFilename: '[name].js',
publicPath: 'assets/js/'
},
plugins: [
new BundleTracker({ filename: './webpack-stats.json' }),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
filename: '[name].js',
minChunks: module => /node_modules/.test(module.resource)
}),
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false
}),
new webpack.optimize.UglifyJsPlugin({
beautify: false,
mangle: {
screw_ie8: true,
keep_fnames: true
},
compress: {
screw_ie8: true,
warnings: false
},
comments: false
}),
new BundleAnalyzer({ analyzerMode: 'static' })
],
resolve: {
modules: ['node_modules'],
extensions: ['*', '.js', '.jsx']
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
options: {
plugins: [
[
'transform-semantic-ui-react-imports',
{
convertMemberImports: true,
addCssImports: false,
importMinifiedCssFiles: false,
addLessImports: false,
addDuplicateStyleImports: false
}
]
],
presets: [['es2015', { modules: false }], 'react', 'stage-2', 'node6']
}
}
]
},
node: {
console: true,
fs: 'empty',
net: 'empty',
tls: 'empty'
}
};

主页组件 |主页/index.js

import React, { Component, PropTypes } from 'react';
import LayoutContainer from '../../containers/LayoutContainer';
import Hero from './Hero';
import About from './About';
import Working from './Working';

class Homepage extends Component {
render() {
return (
<LayoutContainer scrollBound={600}>
<div className="homepage-container">
<p>Here</p>
<Hero />
<About />
<Working />
</div>
</LayoutContainer>
);
}
}

export default Homepage;

最佳答案

您并非在所有情况下都调用回调,因此组件不会返回到路由。例如,这个:

<Route
path="/login"
getComponent={(nextState, callback) => {
require.ensure(['../containers/LoginContainer'], require => {
require('../containers/LoginContainer');
}, 'login');
}}
/>

...应该是:

<Route
path="/login"
getComponent={(nextState, callback) => {
require.ensure(['../containers/LoginContainer'], require => {
callback(null, require('../containers/LoginContainer'));
}, 'login');
}}
/>

关于javascript - react 路由器|组件未渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47981986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com