遇到了問題匯總:
1.請求資源地址錯誤: 在webpack配置中的/config/index.js中的 build.assetsPublicPath中修改路徑
- 直接訪問index.html空白:
可以通過設(shè)置base一般是路由的問題, 如果是根目錄, 應(yīng)該不會有這樣的問題
- 刷新當前路由404:
這個需要在nginx中設(shè)置, 把沒有記錄的路徑重定向到index.html,
由于前端路由緣故,單頁面應(yīng)用應(yīng)該放到nginx或者apache迁筛、tomcat等web代理服務(wù)器中转唉,千萬不要直接訪問index.html,同時要根據(jù)自己服務(wù)器的項目路徑更改react或vue的路由地址笼才。
如果說項目是直接跟在域名后面的步责,比如:http://www.sosout.com 拔疚,根路由就是 '/'。
如果說項目是直接跟在域名后面的一個子目錄中的恤左,比如:http://www.sosout.com/children 贴唇,根路由就是 '/children ',不能直接訪問index.html飞袋。
以配置Nginx為例戳气,配置過程大致如下:(假設(shè):
1、項目文件目錄: /mnt/html/spa(spa目錄下的文件就是執(zhí)行了npm run dist 后生成的dist目錄下的文件)
2巧鸭、訪問域名:spa.sosout.com)
進入nginx.conf新增如下配置:
server {
listen 80;
server_name spa.sosout.com;
root /mnt/html/spa;
index index.html;
location ~ ^/favicon\.ico$ {
root /mnt/html/spa;
}
location / {
try_files $uri $uri/ /index.html;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
access_log /mnt/logs/nginx/access.log main;
}
注意事項:
1瓶您、配置域名的話,需要80端口,成功后呀袱,只要訪問域名即可訪問的項目
2贸毕、如果你使用了react-router的 browserHistory 模式或 vue-router的 history 模式,在nginx配置還需要重寫路由:
server {
listen 80;
server_name spa.sosout.com;
root /mnt/html/spa;
index index.html;
location ~ ^/favicon\.ico$ {
root /mnt/html/spa;
}
location / {
try_files $uri $uri/ @fallback;
index index.html;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location @fallback {
rewrite ^.*$ /index.html break;
}
access_log /mnt/logs/nginx/access.log main;
}
為什么要重寫路由夜赵?因為我們的項目只有一個根入口明棍,當輸入類似/home的url時,如果找不到對應(yīng)的頁面寇僧,nginx會嘗試加載index.html摊腋,這是通過react-router或vue-router就能正確的匹配我們輸入的/home路由,從而顯示正確的home頁面嘁傀,如果browserHistory模式或history模式的項目沒有配置上述內(nèi)容歌豺,會出現(xiàn)404的情況。
簡單舉兩個例子心包,一個vue項目一個react項目:
vue項目
import App from '../App'// 首頁const home = r => require.ensure([], () => r(require('../page/home/index')), 'home')// 物流const logistics = r => require.ensure([], () => r(require('../page/logistics/index')), 'logistics')// 購物車const cart = r => require.ensure([], () => r(require('../page/cart/index')), 'cart')// 我的const profile = r => require.ensure([], () => r(require('../page/profile/index')), 'profile')// 登錄界面const login = r => require.ensure([], () => r(require('../page/user/login')), 'login')export default [{ path: '/', component: App, // 頂層路由,對應(yīng)index.html children: [{ path: '/home', // 首頁 component: home }, { path: '/logistics', // 物流 component: logistics, meta: { login: true } }, { path: '/cart', // 購物車 component: cart, meta: { login: true } }, { path: '/profile', // 我的 component: profile }, { path: '/login', // 登錄界面 component: login }, { path: '*', redirect: '/home' }] }]
############ # 其他配置 ############ http { ############ # 其他配置 ############ server { listen 80; server_name tb.sosout.com; root /mnt/html/tb; index index.html; location ~ ^/favicon\.ico$ { root /mnt/html/tb; } location / { try_files $uri $uri/ @fallback; index index.html; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } location @fallback { rewrite ^.*$ /index.html break; } access_log /mnt/logs/nginx/access.log main; } ############ # 其他配置 ############ }
react項目
/** * 疑惑一: * React createClass 和 extends React.Component 有什么區(qū)別? * 之前寫法: * let app = React.createClass({ * getInitialState: function(){ * // some thing * } * }) * ES6寫法(通過es6類的繼承實現(xiàn)時state的初始化要在constructor中聲明): * class exampleComponent extends React.Component { * constructor(props) { * super(props); * this.state = {example: 'example'} * } * } */import React, {Component, PropTypes} from 'react'; // react核心import { Router, Route, Redirect, IndexRoute, browserHistory, hashHistory } from 'react-router'; // 創(chuàng)建route所需import Config from '../config/index'; import layout from '../component/layout/layout'; // 布局界面import login from '../containers/login/login'; // 登錄界面/** * (路由根目錄組件馒铃,顯示當前符合條件的組件) * * @class Roots * @extends {Component} */class Roots extends Component { render() { // 這個組件是一個包裹組件蟹腾,所有的路由跳轉(zhuǎn)的頁面都會以this.props.children的形式加載到本組件下 return ( <div>{this.props.children}</div> ); } }// const history = process.env.NODE_ENV !== 'production' ? browserHistory : hashHistory;// 快速入門const home = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/home/homeIndex').default) }, 'home'); } // 百度圖表-折線圖const chartLine = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/charts/lines').default) }, 'chartLine'); }// 基礎(chǔ)組件-按鈕const button = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/general/buttonIndex').default) }, 'button'); }// 基礎(chǔ)組件-圖標const icon = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/general/iconIndex').default) }, 'icon'); }// 用戶管理const user = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/user/userIndex').default) }, 'user'); }// 系統(tǒng)設(shè)置const setting = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/setting/settingIndex').default) }, 'setting'); }// 廣告管理const adver = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/adver/adverIndex').default) }, 'adver'); }// 組件一const oneui = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/ui/oneIndex').default) }, 'oneui'); }// 組件二const twoui = (location, cb) => { require.ensure([], require => { cb(null, require('../containers/ui/twoIndex').default) }, 'twoui'); }// 登錄驗證const requireAuth = (nextState, replace) => { let token = (new Date()).getTime() - Config.localItem('USER_AUTHORIZATION'); if(token > 7200000) { // 模擬Token保存2個小時 replace({ pathname: '/login', state: { nextPathname: nextState.location.pathname } }); } }const RouteConfig = ( <Router history={browserHistory}> <Route path="/home" component={layout} onEnter={requireAuth}> <IndexRoute getComponent={home} onEnter={requireAuth} /> // 默認加載的組件,比如訪問www.test.com,會自動跳轉(zhuǎn)到www.test.com/home <Route path="/home" getComponent={home} onEnter={requireAuth} /> <Route path="/chart/line" getComponent={chartLine} onEnter={requireAuth} /> <Route path="/general/button" getComponent={button} onEnter={requireAuth} /> <Route path="/general/icon" getComponent={icon} onEnter={requireAuth} /> <Route path="/user" getComponent={user} onEnter={requireAuth} /> <Route path="/setting" getComponent={setting} onEnter={requireAuth} /> <Route path="/adver" getComponent={adver} onEnter={requireAuth} /> <Route path="/ui/oneui" getComponent={oneui} onEnter={requireAuth} /> <Route path="/ui/twoui" getComponent={twoui} onEnter={requireAuth} /> </Route> <Route path="/login" component={Roots}> // 所有的訪問区宇,都跳轉(zhuǎn)到Roots <IndexRoute component={login} /> // 默認加載的組件娃殖,比如訪問www.test.com,會自動跳轉(zhuǎn)到www.test.com/home </Route> <Redirect from="*" to="/home" /> </Router> ); export default RouteConfig;
############ # 其他配置 ############ http { ############ # 其他配置 ############ server { listen 80; server_name antd.sosout.com; root /mnt/html/reactAntd; index index.html; location ~ ^/favicon\.ico$ { root /mnt/html/reactAntd; } location / { try_files $uri $uri/ @router; index index.html; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } location @router { rewrite ^.*$ /index.html break; } access_log /mnt/logs/nginx/access.log main; } ############ # 其他配置 ############ }