2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > vue react等单页面项目部署到服务器方法

vue react等单页面项目部署到服务器方法

时间:2021-05-29 17:57:26

相关推荐

vue react等单页面项目部署到服务器方法

web前端|js教程

react,服务器,部署

web前端-js教程

用vue做的项目本地是可以的,但部署到服务器遇到好多问题:资源找不到直接访问index.html页面空白刷新当前路由404。本文我们主要和大家分享vue、react等单页面项目部署到服务器方法,希望能帮助到大家。

征途源码,ubuntu使用n卡,Socks5爬虫,左侧php,进入页 seolzw

由于前端路由缘故,单页面应用应该放到nginx或者apache、tomcat等web代理服务器中,千万不要直接访问index.html,同时要根据自己服务器的项目路径更改react或vue的路由地址。

.net源码 调查问卷,ubuntu g 怎么升级,Tomcat配置文件有中文,陶瓷爬虫灯,php怎么制作管理网页,潘坚seolzw

如果说项目是直接跟在域名后面的,比如: ,根路由就是 ‘/’。

如果说项目是直接跟在域名后面的一个子目录中的,比如:/children ,根路由就是 ‘/children ‘,不能直接访问index.html。

易语言登录源码,vscode输出乱码,ubuntu上网,tomcat 是什么,sqlite创建数据库,网页设计师做什么,oracle数据库远程连接,ftp服务器链接不成功,添加主屏幕全屏插件,前端框架使用,node 爬虫,php 服务器,seo关键词优化,springboot开源项目,php 给span标签赋值,网站建设素材,网页图片随屏幕移动,discuz分类信息 列表页模板,扁平化设计后台模板,jquery弹出层加载页面,会议室管理系统模板,播放声音c 程序lzw

以配置Nginx为例,配置过程大致如下:(假设:

1、项目文件目录: /mnt/html/spa(spa目录下的文件就是执行了npm run dist 后生成的dist目录下的文件)

2、访问域名:)

进入nginx.conf新增如下配置:

server { listen 80; server_name ; 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 ; 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时,如果找不到对应的页面,nginx会尝试加载index.html,这是通过react-router就能正确的匹配我们输入的/home路由,从而显示正确的home页面,如果browserHistory模式或history模式的项目没有配置上述内容,会出现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, // 顶层路由,对应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 ; 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 ponent 有什么区别?* 之前写法:* let app = React.createClass({*getInitialState: function(){* // some thing*}* })* ES6写法(通过es6类的继承实现时state的初始化要在constructor中声明):* class exampleComponent extends ponent {* constructor(props) {* super(props);* this.state = {example: example}* }* }*/import React, {Component, PropTypes} from eact; // react核心import { Router, Route, Redirect, IndexRoute, browserHistory, hashHistory } from eact-router; // 创建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() { // 这个组件是一个包裹组件,所有的路由跳转的页面都会以this.props.children的形式加载到本组件下 return (

{this.props.children}

); }}// 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);}// 基础组件-按钮const button = (location, cb) => { require.ensure([], require => { cb(null, require(../containers/general/buttonIndex).default) }, utton);}// 基础组件-图标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);}// 系统设置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) }, woui);}// 登录验证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 = (// 默认加载的组件,比如访问,会自动跳转到/home// 所有的访问,都跳转到Roots // 默认加载的组件,比如访问,会自动跳转到/home);export default RouteConfig;

############# 其他配置############http { ############ # 其他配置 ############ server { listen 80; server_name ; 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; } ############ # 其他配置 ############ }

如何使用Docker部署PHP开发环境

实例讲解PHP实现git部署

PHP设计模式之基于模板引擎的容器部署框架

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。