vue、react等單頁(yè)面項(xiàng)目部署到服務(wù)器的方法及vue和react的區(qū)別
最近好多伙伴說(shuō),我用vue做的項(xiàng)目本地是可以的,但部署到服務(wù)器遇到好多問(wèn)題:資源找不到,直接訪(fǎng)問(wèn)index.html頁(yè)面空白,刷新當(dāng)前路由404。。。用react做的項(xiàng)目也同樣遇到類(lèi)似問(wèn)題?,F(xiàn)在我們一起討論下單頁(yè)面如何部署到服務(wù)器?
由于前端路由緣故,單頁(yè)面應(yīng)用應(yīng)該放到nginx或者apache、tomcat等web代理服務(wù)器中,千萬(wàn)不要直接訪(fǎng)問(wèn)index.html,同時(shí)要根據(jù)自己服務(wù)器的項(xiàng)目路徑更改react或vue的路由地址。
如果說(shuō)項(xiàng)目是直接跟在域名后面的,比如:http://www.sosout.com ,根路由就是 '/'。
如果說(shuō)項(xiàng)目是直接跟在域名后面的一個(gè)子目錄中的,比如: http://www.sosout.com/children ,根路由就是 '/children ',不能直接訪(fǎng)問(wèn)index.html。
以配置Nginx為例,配置過(guò)程大致如下:(假設(shè):
1、項(xiàng)目文件目錄: /mnt/html/spa(spa目錄下的文件就是執(zhí)行了npm run dist 后生成的dist目錄下的文件)
2、訪(fǎng)問(wèn)域名:spa.sosout.com)
進(jìn)入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;
}
注意事項(xiàng):
1、配置域名的話(huà),需要80端口,成功后,只要訪(fǎng)問(wèn)域名即可訪(fǎng)問(wèn)的項(xiàng)目
2、如果你使用了react-router的 browserHistory 模式或 vue-router的 history 模式,在nginx配置還需要重寫(xiě)路由:
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;
}
為什么要重寫(xiě)路由?因?yàn)槲覀兊捻?xiàng)目只有一個(gè)根入口,當(dāng)輸入類(lèi)似/home的url時(shí),如果找不到對(duì)應(yīng)的頁(yè)面,nginx會(huì)嘗試加載index.html,這是通過(guò)react-router或vue-router就能正確的匹配我們輸入的/home路由,從而顯示正確的home頁(yè)面,如果browserHistory模式或history模式的項(xiàng)目沒(méi)有配置上述內(nèi)容,會(huì)出現(xiàn)404的情況。
簡(jiǎn)單舉兩個(gè)例子,一個(gè)vue項(xiàng)目一個(gè)react項(xiàng)目:
vue項(xiàng)目:

############
# 其他配置
############
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;
}
############
# 其他配置
############
}

import App from '../App'
// 首頁(yè)
const home = r => require.ensure([], () => r(require('../page/home/index')), 'home')
// 物流
const logistics = r => require.ensure([], () => r(require('../page/logistics/index')), 'logistics')
// 購(gòu)物車(chē)
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, // 頂層路由,對(duì)應(yīng)index.html
children: [{
path: '/home', // 首頁(yè)
component: home
}, {
path: '/logistics', // 物流
component: logistics,
meta: {
login: true
}
}, {
path: '/cart', // 購(gòu)物車(chē)
component: cart,
meta: {
login: true
}
}, {
path: '/profile', // 我的
component: profile
}, {
path: '/login', // 登錄界面
component: login
}, {
path: '*',
redirect: '/home'
}]
}]
react項(xiàng)目:

/**
* 疑惑一:
* React createClass 和 extends React.Component 有什么區(qū)別?
* 之前寫(xiě)法:
* let app = React.createClass({
* getInitialState: function(){
* // some thing
* }
* })
* ES6寫(xiě)法(通過(guò)es6類(lèi)的繼承實(shí)現(xiàn)時(shí)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'; // 登錄界面
/**
* (路由根目錄組件,顯示當(dāng)前符合條件的組件)
*
* @class Roots
* @extends {Component}
*/
class Roots extends Component {
render() {
// 這個(gè)組件是一個(gè)包裹組件,所有的路由跳轉(zhuǎn)的頁(yè)面都會(huì)以this.props.children的形式加載到本組件下
return (
<div>{this.props.children}</div>
);
}
}
// const history = process.env.NODE_ENV !== 'production' ? browserHistory : hashHistory;
// 快速入門(mén)
const home = (location, cb) => {
require.ensure([], require => {
cb(null, require('../containers/home/homeIndex').default)
}, 'home');
}
// 百度圖表-折線(xiàn)圖
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ǔ)組件-圖標(biāo)
const icon = (location, cb) => {
require.ensure([], require => {
cb(null, require('../containers/general/iconIndex').default)
}, 'icon');
}
// 用戶(hù)管理
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');
}
// 登錄驗(yàn)證
const requireAuth = (nextState, replace) => {
let token = (new Date()).getTime() - Config.localItem('USER_AUTHORIZATION');
if(token > 7200000) { // 模擬Token保存2個(gè)小時(shí)
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} /> // 默認(rèn)加載的組件,比如訪(fǎng)問(wèn)www.test.com,會(huì)自動(dòng)跳轉(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}> // 所有的訪(fǎng)問(wèn),都跳轉(zhuǎn)到Roots
<IndexRoute component={login} /> // 默認(rèn)加載的組件,比如訪(fǎng)問(wèn)www.test.com,會(huì)自動(dòng)跳轉(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;
}
############
# 其他配置
############
}
下面看下vue和react區(qū)別
前端都知道3個(gè)主流框架,vue,react,anjular,當(dāng)然目前最火的還是vue和react,那么vue 和react 的區(qū)別?
相同點(diǎn):
1.都支持服務(wù)器端渲染
2.都有Virtual DOM,組件化開(kāi)發(fā),通過(guò)props參數(shù)進(jìn)行父子組件數(shù)據(jù)的傳遞,都實(shí)現(xiàn)webComponent規(guī)范
3.數(shù)據(jù)驅(qū)動(dòng)視圖
4.都有支持native的方案,React的React native,Vue的weex
5.都有管理狀態(tài),React有redux,Vue有自己的Vuex(自適應(yīng)vue,量身定做)
不同點(diǎn):
1.React嚴(yán)格上只針對(duì)MVC的view層,Vue則是MVVM模式
2.virtual DOM不一樣,vue會(huì)跟蹤每一個(gè)組件的依賴(lài)關(guān)系,不需要重新渲染整個(gè)組件樹(shù).
而對(duì)于React而言,每當(dāng)應(yīng)用的狀態(tài)被改變時(shí),全部組件都會(huì)重新渲染,所以react中會(huì)需要shouldComponentUpdate這個(gè)生命周期函數(shù)方法來(lái)進(jìn)行控制
3.組件寫(xiě)法不一樣, React推薦的做法是 JSX + inline style, 也就是把HTML和CSS全都寫(xiě)進(jìn)JavaScript了,即'all in js';
Vue推薦的做法是webpack+vue-loader的單文件組件格式,即html,css,jd寫(xiě)在同一個(gè)文件;
4.數(shù)據(jù)綁定: vue實(shí)現(xiàn)了數(shù)據(jù)的雙向綁定,react數(shù)據(jù)流動(dòng)是單向的
5.state對(duì)象在react應(yīng)用中不可變的,需要使用setState方法更新?tīng)顟B(tài);
在vue中,state對(duì)象不是必須的,數(shù)據(jù)由data屬性在vue對(duì)象中管理;
就對(duì)我而言吧,vue適合開(kāi)發(fā)移動(dòng)端項(xiàng)目,react適合開(kāi)發(fā)pc端項(xiàng)目(個(gè)人觀點(diǎn)),
當(dāng)然我還是喜歡 React,畢竟后臺(tái)大,哈哈,雖然現(xiàn)在升級(jí)到16版本了(不喜勿噴)
總結(jié)
以上所述是小編給大家介紹的vue、react等單頁(yè)面項(xiàng)目應(yīng)用部署到服務(wù)器的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue2.x+webpack快速搭建前端項(xiàng)目框架詳解
本文給大家介紹了vue2.x、webpack、vuex、sass+axios、elementUI等快速搭建前端項(xiàng)目框架的詳細(xì)操作方法,需要的跟著學(xué)習(xí)下吧。2017-11-11
vue打包之后配置統(tǒng)一請(qǐng)求地址步驟詳解
這篇文章主要為大家介紹了vue打包之后配置統(tǒng)一請(qǐng)求地址實(shí)現(xiàn)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Vue中computed和watch的區(qū)別小結(jié)
watch和computed都是以Vue的依賴(lài)追蹤機(jī)制為基礎(chǔ)的,當(dāng)某一個(gè)依賴(lài)型數(shù)據(jù)發(fā)生變化的時(shí)候,所有依賴(lài)這個(gè)數(shù)據(jù)的相關(guān)數(shù)據(jù)會(huì)自動(dòng)發(fā)生變化,即自動(dòng)調(diào)用相關(guān)的函數(shù),來(lái)實(shí)現(xiàn)數(shù)據(jù)的變動(dòng),這篇文章簡(jiǎn)單介紹下Vue中computed和watch的區(qū)別異同,感興趣的朋友一起看看吧2022-12-12
vue中@click綁定事件點(diǎn)擊不生效的原因及解決方案
根據(jù)Vue2.0官方文檔關(guān)于父子組件通訊的原則,父組件通過(guò)prop傳遞數(shù)據(jù)給子組件,子組件觸發(fā)事件給父組件,這篇文章主要介紹了vue中@click綁定事件點(diǎn)擊不生效的解決方案,需要的朋友可以參考下2022-12-12
nuxt 頁(yè)面路由配置,主頁(yè)輪播組件開(kāi)發(fā)操作
這篇文章主要介紹了nuxt 頁(yè)面路由配置,主頁(yè)輪播組件開(kāi)發(fā)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
基于mpvue的簡(jiǎn)單彈窗組件mptoast使用詳解
這篇文章主要介紹了基于mpvue的簡(jiǎn)單彈窗組件mptoast使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
import.meta.glob() 如何導(dǎo)入多個(gè)目錄下的資源(最新推薦)
import.meta.glob() 其實(shí)不僅能接收一個(gè)字符串,還可以接收一個(gè)字符串?dāng)?shù)組,就是匹配多個(gè)位置,本文給大家介紹import.meta.glob() 如何導(dǎo)入多個(gè)目錄下的資源,感興趣的朋友一起看看吧2023-11-11

