vue、react等單頁面項(xiàng)目應(yīng)該這樣子部署到服務(wù)器_第1頁
vue、react等單頁面項(xiàng)目應(yīng)該這樣子部署到服務(wù)器_第2頁
vue、react等單頁面項(xiàng)目應(yīng)該這樣子部署到服務(wù)器_第3頁
vue、react等單頁面項(xiàng)目應(yīng)該這樣子部署到服務(wù)器_第4頁
vue、react等單頁面項(xiàng)目應(yīng)該這樣子部署到服務(wù)器_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

服務(wù)端渲染:next,js、nuxl.js等服務(wù)端泊染框架構(gòu)建的項(xiàng)II部署到服務(wù)器,并用PM2守護(hù)程序最近好多伙伴說,我用vue做的項(xiàng)目本地是可以的,但部署到服務(wù)器遇到好多問題:個(gè)源找不到,立接訪問MdexAt3/頁向空白,刷新當(dāng)前路由404。。。用react做的項(xiàng)目也同樣遇到類似問題?,F(xiàn)在我們一起討論下單頁面如何部署到服務(wù)器?由于前端路由緣故,單頁面應(yīng)用應(yīng)該放到nginx或者apache、tomcat等web代理服務(wù)器中,千萬不要直接訪問index.htmL同時(shí)要根據(jù)自己服務(wù)器的項(xiàng)目路徑更改react或vue的路由地址。如果說項(xiàng)目是直接跟在域名后面的,比如:http:〃,根路由就是7。如果說項(xiàng)目是直接跟在域名后面的一個(gè)子目錄中的,比$n:/children,根路由就是'/children不能直接訪問index.htmlo以配置Nginx為例,配置過程大致如下:(假設(shè):1、項(xiàng)目文件目錄:/mnt/html/spa(spa目錄下的文件就是執(zhí)行了npmrundist后生成的dist目錄下的文件)2、訪問域名:)進(jìn)入nginx.conf新增如下配置:server{listen80;serverja4nAespq.sosout.coM;rootindexindex.hti^l;location?A/faWco八\.ico${root/kv\nt/htkw//spa;)location/{tryJiles iuri/proxy_$et_keadeirHost 物ost;prox”et_〃eaderX-Real-IP ^rekv\ote_addr;proxy_$et_headeirX-Forwarded-For proxy_add_xjor\A/arded_for;proxy_$et_headerX-Forwarded-ProtoSsMenAC;)accessjog/Met/(og,/ngi八x/access.(og出〃,八;)注意事項(xiàng):工、配置.域名的話,需要80端口,成功后,只要訪問域名即可訪問的項(xiàng)目2、如果你使用了“act-丫。er的bro\A/serHistory模式或vue-fouter*的kistory模式,在八g/'nx配置還需要重寫路由:server{listen80;server_Hakv\espasosout.co^root/Mnt/htM〔/spa;index心dex.ktiW;location?人/fas'c。n\.ico${root/kv\nt/htkv\l/$pa;}location/{tryjile^$uri$un7^fallback;indexindex.kt^v\l;proxy_$et_keaderHost$host;proxyX-Real-IP$re^ote_addr;proxy_$et_headerproxy_$et_headerX-Forwarded-For$proxy_add_xjor\A/arded_for;proxy_$et_headerproxy_$et_headerX-Forwarded-Proto$sc〃e&e;location^fallback{rewrite人.*$/index.htiHbreak;accessjog/Mnt/1ogs/nginx/access,log kv\aiia;為什么要重寫路由?因?yàn)槲覀兊捻?xiàng)H只有一個(gè)根入口,當(dāng)輸入類似/hoke的u”時(shí),如果找不到對(duì)應(yīng)的頁面,cgicx會(huì)嘗試加載rndex.htkwl,這是通過react-router或vue-router就能正確的匹配我們輸入的/人。26路由,從而顯示正確的kowve頁面,如果ferowserHrstory模式或kkt"g模式的項(xiàng)目沒有配置上述內(nèi)容,會(huì)出現(xiàn)404的情況。簡單舉兩個(gè)例子,一個(gè)vue項(xiàng)目一個(gè)react項(xiàng)目:vue項(xiàng)目:域名:Branch:master▼vue-taobao/src/router/index.jsOsosout登錄攔截1contributor47lines(40sloe)1.04KBimportAppfrom1../App(//首頁consthome=r=>require.ensure([],()=>r(require(*../page/home/index1)),'home')56 //物濠constlogistics=r=>require.ensure((],()=>r(require(,../page/logistics/index,)),9 //購物車constcart=r=>require.ensure([]9()=>r(require(a?./page/cart/index'))r*cart,)12 //我的constprofile=r=>require.ensure((],()=>r(require(*../page/profile/index*))9*pr1414 //importAppfrokv\L/App'//首頁constkokvxe=r=>require.enured],()=>rfrequire(,../page/ho^e/iiade>x,y),//物流coiastlogistics=r=>八sure(口,0=>dequi必../page/logistics/i八dex?logistics')//購物彳constcart=r=> nsure(口,0=>r(reqaire(*../page/cart/i^dex1)),'cart1)//我的constprofile=r=>require.enquired],。=>vXirequireC../page/profHe/profile1)//冷泉界面constlogin=r=> 0=>r(requiire(,../page/u^er/logiia,y)Jlogin1)exportdefault[{

path:yscomponent:Appj//頂層路由,對(duì)應(yīng)以children:[{patk;,/koMeL〃首頁component:hokweL(path'/logistics^//物流coi^poMnt:logistics,0A比〃;{logivt\true}}>(pathVcart',//購物車component:cart}^eta:(login:true)L(path'/profile1,//我的component:profile)>(path:l/login,J//登錄界ifii

234671011,*>13141516171819212223242526272829303132333435cokvxpoMnt:logMpath:redirect:}]〃(runtime-onlyorstandalone)hasbeensetinwebpack.base.confwithanalias,importVuefrom*vue,importVueRouterfrom1vue-router'importroutesfrom?./router/index,importFastClickfrom'fastclick'importStoragefrom?store1importConfigfrom?./config/index,importstorefrom?./store/1import1./config/rem1if('addEventListener*indocument){document.addEventListener(1DOMContentLoaded',function(){FastClick.attach(document.body)})VductionTip=falseVue.use(VueRouter)constrouter=newVueRouter({routes,mode:Config.routerMode,strict:process.env.N0DE_ENV!=?production*fscrollBehavior(to,from,savedPosition){if(savedPosition){returnsavedPosition}else{if(from.meta.keepAlive){from.meta.savedPosition=document.body.scrollTop)return{x:0,y:to.meta.savedPosition||0}}>

dI-沖&-X J 仆k6xo/d?5%/空"vp*uGxo/df/wj^vjxapuix9pvi"99印/四*uv)t5刃”一心3}/MOI^VOO){叩/WS/JUWI/Q.OOJ}g031\U。/八ty/v?M0RD20)f/wjv/x^pwxapuj用力皿川/川皿/j。。/fW。),力。5。5'弓3 2W17VO2/U35f08M鄧!))J3ZJ95############MWM#############}d*############基園訓(xùn)苴#############ujo3,inosos,piue//:duq:/狗s目.Peaj(############曷國訓(xùn)苴#############(fvJUWi6o/5S2"l?/Xvfj6u/56q/?uvvi/6。廠552%V{f型2/qJWJVfXapu,/}”嗎〃ty@U0R8。][1?W2vp5'O^OJ^j-p^pJVPAJO^-X /叩功VWkGxO/d□ojrp9pjv/v\joj~)Tppv~fi^ojdfjoj-p^pjvMJoj-x/卯ba"-"/*。//

Branch:master-react-antd/src/router/route.jsxOsosout引入百度圖我、修改webpack配置1contributor139lines(122sloe)4.27KB/4疑惑一:*ReactcreateClass疑惑一:*ReactcreateClass和extendsReact.Coyv\poM^t有什么區(qū)別?*之前寫法:letapp=React.createClass([?ReactcreateClass和extendsReact.Component有什么區(qū)別??之前寫法:*letapp=React?createClass({?getlnitialState:function(){? //something? }?})*ES6寫法(通過es6類的繼承實(shí)現(xiàn)時(shí)state的初始化要在constructor中聲明):*classexamplecomponentextendsReact.Component{? constructor(props){? super(props);* this.state={example:?example,}TOC\o"1-5"\h\z? }?}*/importReact,{Component,PropTypes}from?react1;//react核心import{Router,Route,Redirect,IndexRoute,browserHistory,hashHistory}froi2importConfigfrom1../config/index1;importlayoutfrom1?./component/layout/layout1;//布局界面23importloginfrom?../containers/login/login*;//fl1錄界面

getlnitialState:f(AMtion(){//soMething}1)ES6寫法(通過es6類的繼承實(shí)現(xiàn)時(shí)state的初始化要在constructor中聲明):classexai^pleCoi^poM^textendsReact.Coi^poM^t{construetorfprops){super(props);tkisstate-{example:'example1]* )*)*/importReact,[Coi^poM^tjPropTypes}froha'reacts//react核心、import(Router^Route,Redirectj伍dexRou七e,bfowse/Histo嗎,hashHistory}frohA1react-router1;//創(chuàng)建route所@c/assRoots*@exte八ds{Coi^poMnt}importConfigfrow’../config/i八4ex';@c/assRoots*@exte八ds{Coi^poMnt}importlogicfroka'../co^taiMrs/logi^/logi^1;//:)」《界面/★★/classRootsextendsCom?。nent(*(路由根目錄組件,顯示當(dāng)前符合條件的組件)render(){//這個(gè)組件是一個(gè)包裹組件,所有的路由跳轉(zhuǎn)的頁面都會(huì)以痂4)"。眸沙血七八的形式加載到本組件下return(<div>{p^.ckildre^</div>);))//consthisto/g=pscess.e八v.NODE_ENV/=='production1?bro\A/serHistory:ka$Mi$tory;//快速入門consthowe=(location,do)=>{re^u/re.ensureff],require=>{cb^ulljrequire(,../contaiMrs/hoi^e/ho^eliadexl).default))//百度圖表-折線圖constchartUM=(location,do)=>{requi/e.ens〃re(口,require=>{ct(nu(Gredjuiref../contaiMr^/charts/liMS^.default)},'chartUM');}//基礎(chǔ)組件-按鈕constbutton-(location,cb)=>{require.ei^sure(1]jrequire=>{cb(^ulljrequire(,../contaiMrs/geMral/buttonlk\dex,).default)'button');1//基礎(chǔ)組件-圖標(biāo)consticon=(location,cb)=>{re^u/re.ensureff],require=>{cb(八川Lrequire(,../contaiMrs/gcMral/ico^l^dex').default)}JicorC);)//用戶管理constuser=(locatio^jcb)=>{requiw.e八sure(0require=>{cb(nulljrequireC../contaiMrs/user/userlnde^ydefault)}J〃seL);)//系統(tǒng)設(shè)置constsetting=(location,cb)=>{require.ensuredJjrequire=>{c優(yōu)八川。require(l../co^taiMrs/setting/settii^glndex,).default)'setting');}//廣告管理constadver=(location,cb)->{reiju/re.ensureff],require=>{cb(nulljrecjui^e(,../contaiMrs/adver/adverliadex,).default^)},Wver1);)//組件constoiaeui=(locatioi^jcb)=>{requfre.ensureft],require=>{cb(^ulljrequire(,../contaiMrs/ui/oMliadex,).defauli)}j'oMui');}//組件,co八sttwouf=(location,cb)=>{re^u/re.ensure([]?require=>{cb?川。require(,../co^taiMrs/ui/t\A/olndex').default)}j't\A/oui');)//登錄驗(yàn)證constre^u/reAuth=(nextSt^te,replace)=>{lettoken=?ewDate(y).getTi^O-Config.localltekv\(,USER_AUTHORIZATION,);if(token>7200000){//模擬Token保存2個(gè)小時(shí)trep(ace({pathiaai^e:'/logi^jstate:[nextPathnakvxe:MxtState.location.patkiaai^e>}});}}constRouteCoiafig=(<Routerhistory={browserHistory)>〈Routepath='7ho^e"cokv\poM^t-{layout]。八E八ter={requiKeAutk}>〈次dexRoutegetCokvxp。八e八七二{honae}onEnter={re6ju/reAuth}/>//默認(rèn)加載的組件,比如訪問www.test.con/b會(huì)自動(dòng)跳轉(zhuǎn)到www.test.conVhokne〈Routepath="/〃omc"getCoi<v\poMiat={ko^e}onEHter={requ/reAut/i}/><Routep?tk="/chart/1i八e"getCoi^poMiat={chartLiM]OHEnter={re^u/reAuth)/><Routepath="/geMra1/button"getCoi^poM^t-{butto^}(mE八ter={requi%Aug}/><Routepath="/geneiral/ico八"getCoi^poM^t-{icoiK}on.E八七er={requ"eA〃t〃}/><Routepatk-,,/useri,getrCokwpo八e八七={userjonEEvter={requ/reAuth)/><RoutepatK="/setting"getCo^poMi^t-{settiiag]。八Entcm&equireAu聞/><Routepath^'/adve^1getCo^v\poMiat-{adver\OHEnter={requ/reAuth}/><Routepatk-"/ui/oMui"getCo^v\poMiat-{oMui]。八E八/><Routepath="/ui/t\A/ouingetCo^v\poMi^t-{t\A/oui]。八E八ter={%qu,HeAuth}/></Route><Routepath="/logi八"c。嗎?。八e八t={Roots}>//所有的訪問,都跳轉(zhuǎn)到Roots<lndexR.outecoMpone八七二{/og/'k}/>//默認(rèn)加載的組件,比如訪問www.test.cok,會(huì)自動(dòng)跳轉(zhuǎn)到/hoFwe</Route><Redirectfro3="*"to="/how\e"/></Router>}JQAA?S############曷四訓(xùn)"#############}d中############曷國訓(xùn)苴#############((?MP. >op)pi[qjpuXdQ-Apoq?:uMn>op?<JdpTAOJd/>{八noj} M<{djo^s)?ajo)sjdpjAOJd>)jdpusjst“附官■用//LX“《STI(()d)e)$)d6*ajo)s)601(aiosuo3// ri多姐息//!<?())dqTJ3sqn

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論