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;
    #    }
    #}

}
相关推荐
ZC跨境爬虫1 分钟前
跟着 MDN 学 HTML day_17:媒体与 Web Audio API 自动播放指南——策略、检测与最佳实践
前端·笔记·ui·html·音视频·媒体
canonical_entropy6 分钟前
Nop Chaos Flux:百度AMIS之后的下一代低代码渲染引擎
前端·低代码·ai编程
时光足迹25 分钟前
Tiptap 简单编辑器模版
前端·javascript·react.js
Cyber4K29 分钟前
【Python专项】Nginx访问日志分析时间范围处理示例
开发语言·python·nginx
JSLove38 分钟前
nginx入门
前端·nginx
时光足迹39 分钟前
ThreeJS之GUI控制器
前端·javascript·three.js
时光足迹40 分钟前
Tiptap编辑器
前端·javascript·react.js
时光足迹44 分钟前
电子书阅读器之笔记高亮(跨段处理)
前端·javascript·react.js
Dabei1 小时前
Android 副屏(Virtual Display)创建与悬浮窗画中画显示实战
前端·架构