亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue、react等單頁面項目部署到服務(wù)器的方法及vue和react的區(qū)別

 更新時間:2018年09月29日 14:20:12   作者:每天一探  
這篇文章主要介紹了vue、react等單頁面項目部署到服務(wù)器的方法,需要的朋友可以參考下

最近好多伙伴說,我用vue做的項目本地是可以的,但部署到服務(wù)器遇到好多問題:資源找不到,直接訪問index.html頁面空白,刷新當(dāng)前路由404。。。用react做的項目也同樣遇到類似問題?,F(xiàn)在我們一起討論下單頁面如何部署到服務(wù)器?

由于前端路由緣故,單頁面應(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;
}

為什么要重寫路由?因為我們的項目只有一個根入口,當(dāng)輸入類似/home的url時,如果找不到對應(yīng)的頁面,nginx會嘗試加載index.html,這是通過react-router或vue-router就能正確的匹配我們輸入的/home路由,從而顯示正確的home頁面,如果browserHistory模式或history模式的項目沒有配置上述內(nèi)容,會出現(xiàn)404的情況。

簡單舉兩個例子,一個vue項目一個react項目:

vue項目:

域名:http://tb.sosout.com

############
# 其他配置
############

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'

// 首頁
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'
 }]
}]

react項目:

域名:http://antd.sosout.com

/**
* 疑惑一:
* 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'; // 登錄界面

/**
 * (路由根目錄組件,顯示當(dāng)前符合條件的組件)
 * 
 * @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ǔ)組件-圖標(biāo)
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;
 }
 ############
 # 其他配置
 ############ 
}

下面看下vue和react區(qū)別

前端都知道3個主流框架,vue,react,anjular,當(dāng)然目前最火的還是vue和react,那么vue 和react 的區(qū)別?

相同點:

    1.都支持服務(wù)器端渲染

    2.都有Virtual DOM,組件化開發(fā),通過props參數(shù)進行父子組件數(shù)據(jù)的傳遞,都實現(xiàn)webComponent規(guī)范

    3.數(shù)據(jù)驅(qū)動視圖

    4.都有支持native的方案,React的React native,Vue的weex

    5.都有管理狀態(tài),React有redux,Vue有自己的Vuex(自適應(yīng)vue,量身定做)

不同點:

       1.React嚴格上只針對MVC的view層,Vue則是MVVM模式

       2.virtual DOM不一樣,vue會跟蹤每一個組件的依賴關(guān)系,不需要重新渲染整個組件樹.

           而對于React而言,每當(dāng)應(yīng)用的狀態(tài)被改變時,全部組件都會重新渲染,所以react中會需要shouldComponentUpdate這個生命周期函數(shù)方法來進行控制

       3.組件寫法不一樣, React推薦的做法是 JSX + inline style, 也就是把HTML和CSS全都寫進JavaScript了,即'all in js';

           Vue推薦的做法是webpack+vue-loader的單文件組件格式,即html,css,jd寫在同一個文件;

       4.數(shù)據(jù)綁定: vue實現(xiàn)了數(shù)據(jù)的雙向綁定,react數(shù)據(jù)流動是單向的

       5.state對象在react應(yīng)用中不可變的,需要使用setState方法更新狀態(tài);

         在vue中,state對象不是必須的,數(shù)據(jù)由data屬性在vue對象中管理;

就對我而言吧,vue適合開發(fā)移動端項目,react適合開發(fā)pc端項目(個人觀點),

              當(dāng)然我還是喜歡 React,畢竟后臺大,哈哈,雖然現(xiàn)在升級到16版本了(不喜勿噴)

總結(jié)

以上所述是小編給大家介紹的vue、react等單頁面項目應(yīng)用部署到服務(wù)器的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • vue移動端裁剪圖片結(jié)合插件Cropper的使用實例代碼

    vue移動端裁剪圖片結(jié)合插件Cropper的使用實例代碼

    本篇文章主要介紹了vue移動端裁剪圖片結(jié)合插件Cropper的使用實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • vue-cli監(jiān)聽組件加載完成的方法

    vue-cli監(jiān)聽組件加載完成的方法

    今天小編就為大家分享一篇vue-cli監(jiān)聽組件加載完成的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vue監(jiān)聽Enter鍵的方法總結(jié)與區(qū)別

    Vue監(jiān)聽Enter鍵的方法總結(jié)與區(qū)別

    這篇文章主要給大家介紹了關(guān)于Vue監(jiān)聽Enter鍵的方法與區(qū)別的相關(guān)資料,在Vue中我們可以通過監(jiān)聽鍵盤事件來實現(xiàn)回車鍵切換焦點的功能,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-10-10
  • Vue收集依賴與觸發(fā)依賴源碼刨析

    Vue收集依賴與觸發(fā)依賴源碼刨析

    vue對依賴的管理使用的是發(fā)布訂閱者模式,其中watcher扮演訂閱者,Dep扮演發(fā)布者。所以dep中會有多個watcher,一個訂閱者也可以有多個發(fā)布者(依賴)。總共三個過程:定義依賴、收集依賴、觸發(fā)依賴。下面開始詳細講解三個過程
    2022-10-10
  • vue使用transition組件動畫效果的實例代碼

    vue使用transition組件動畫效果的實例代碼

    這篇文章主要介紹了vue使用transition組件動畫效果的實例代碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • jenkins自動構(gòu)建發(fā)布vue項目的方法步驟

    jenkins自動構(gòu)建發(fā)布vue項目的方法步驟

    這篇文章主要介紹了jenkins自動構(gòu)建發(fā)布vue項目的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 使用 Element UI Table 的 slot-scope方法

    使用 Element UI Table 的 slot-scope方法

    這篇文章主要介紹了使用 Element UI Table 的 slot-scope方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 打通前后端構(gòu)建一個Vue+Express的開發(fā)環(huán)境

    打通前后端構(gòu)建一個Vue+Express的開發(fā)環(huán)境

    這篇文章主要介紹了打通前后端構(gòu)建一個Vue+Express的開發(fā)環(huán)境,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • 基于node+vue實現(xiàn)簡單的WebSocket聊天功能

    基于node+vue實現(xiàn)簡單的WebSocket聊天功能

    最近學(xué)習(xí)了一下websocket的即時通信,感覺非常的強大,這里我用node啟動了一個服務(wù)進行websocket鏈接,然后再vue的view里面進行了鏈接,進行通信,廢話不多說,直接上代碼吧
    2020-02-02
  • 淺談vue中關(guān)于checkbox數(shù)據(jù)綁定v-model指令的個人理解

    淺談vue中關(guān)于checkbox數(shù)據(jù)綁定v-model指令的個人理解

    這篇文章主要介紹了淺談vue中關(guān)于checkbox數(shù)據(jù)綁定v-model指令的個人理解,v-model用于表單的數(shù)據(jù)綁定很常見,下面就來詳細的介紹一下
    2018-11-11

最新評論