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

}
相关推荐
程序员清洒2 小时前
Flutter for OpenHarmony:GridView — 网格布局实现
android·前端·学习·flutter·华为
VX:Fegn08952 小时前
计算机毕业设计|基于ssm + vue超市管理系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·后端·课程设计
0思必得02 小时前
[Web自动化] 反爬虫
前端·爬虫·python·selenium·自动化
LawrenceLan2 小时前
Flutter 零基础入门(二十六):StatefulWidget 与状态更新 setState
开发语言·前端·flutter·dart
秋秋小事2 小时前
TypeScript 模版字面量与类型操作
前端·typescript
2401_892000523 小时前
Flutter for OpenHarmony 猫咪管家App实战 - 添加提醒实现
前端·javascript·flutter
Yolanda943 小时前
【项目经验】vue h5移动端禁止缩放
前端·javascript·vue.js
Mr.朱鹏3 小时前
Nginx路由转发案例实战
java·运维·spring boot·nginx·spring·intellij-idea·jetty
VX:Fegn08954 小时前
计算机毕业设计|基于springboot + vue酒店管理系统(源码+数据库+文档)
vue.js·spring boot·课程设计
广州华水科技4 小时前
单北斗GNSS形变监测一体机在基础设施安全中的应用与技术优势
前端