vue history 模式下 编译多入口文件和 nginx 配置文件

项目场景:

vue 搭建的项目,路由一直用的hash模式,所以 url 中都会带有一个"#"号。现在想要去掉 "#",于是使用 history 模式 { mode: 'history' },代码如下:

javascript 复制代码
import Vue from 'vue';
import App from './App';
import routers from './router';
import VueRouter from 'vue-router';
 
Vue.use(VueRouter);
 
const router = new VueRouter({
  mode: 'history',
  routes: routers
});
 
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: {App},
  template: '<App/>'
});

问题:页面空白

mode: 'history',使用这个模式,在开发阶段一切都是正常的,访问地址是 localhost:8888,以上没什么错误,一切都好用。url 里也没有#了。可是打包之后,访问项目会发现页面一片空白的情况。不是404,不报错,只是空白!!

解决办法:

很多项目都放在了服务器根目录下面,访问后的 url 就是:123.com,这是一种解决办法

假如我的项目没有放在服务器根目录下,放在了服务器的ele下,那么打开地址是:123.com/ele

那么这里问题就来了,我们配的路由中并没有 ele,所以无法找到路径中的组件,所以也就无法渲染了。这就是导致空白的原因!

此时只需要修改 router 中的 index.js,给路由中加一个 base 的属性,值为 '/ele/'就可以了。代码:

csharp 复制代码
const router = new VueRouter({  
  mode: 'history',  
  base: '/ele/',  
  routes: routers  
});

注意:

记住这个 base,base 值两边一定要有"/",不要写成"ele"、"/ele"或者"ele/"。其次,这个文件夹是服务器放项目的文件夹,不是你本地项目的文件夹位置!!

服务器文件也要配置 nginx.conf

ini 复制代码
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #  try_files $uri $uri/ /login.html;

       #  /login.html; 如果有个 ele文件夹 就 /ele/login.html
        location /login {
          root  html;
          index login.html;
          try_files $uri $uri/ /login.html;
       }
        location /admin {
          root  html;
          index admin.html;
          try_files $uri $uri/ /admin.html;
       }
       location / {
          root html;
          index index.html;
          try_files $uri $uri/ /index.html;
       }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
相关推荐
Rverdoser6 分钟前
代理服务器运行速度慢是什么原因
开发语言·前端·php
航Hang*24 分钟前
前端项目2-01:个人简介页面
前端·经验分享·html·css3·html5·webstorm
MaisieKim_37 分钟前
python与nodejs哪个性能高
前端·python·node.js
水煮白菜王1 小时前
深入理解 Webpack 核心机制与编译流程
前端·webpack·node.js
计算机学姐1 小时前
基于SpringBoot的小区停车位管理系统
java·vue.js·spring boot·后端·mysql·spring·maven
梦幻通灵1 小时前
Excel分组计算求和的两种实现方案
前端·excel
geovindu2 小时前
vue3: pdf.js 3.4.120 using javascript
开发语言·javascript·vue.js·pdf
whatever who cares2 小时前
CSS3 伪类和使用场景
前端·css·css3
水银嘻嘻2 小时前
Web 自动化之 HTML & JavaScript 详解
前端·自动化·html
天天打码2 小时前
Lynx-字节跳动跨平台框架多端兼容Android, iOS, Web 原生渲染
android·前端·javascript·ios