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

}
相关推荐
nyf_unknown11 分钟前
(vue)elementUi中el-upload上传附件之后 点击附件可下载
javascript·vue.js·elementui
qq_3325394513 分钟前
React 前端框架推荐
前端·react.js·前端框架
19岁开始学习30 分钟前
cgi,php-cgi,fastcgi,php-fpm,nginx 小记
开发语言·nginx·php
拉不动的猪30 分钟前
刷刷题34(uniapp中级实际项目问题-1)
前端·vue.js·面试
奔跑的露西ly1 小时前
【HarmonyOS NEXT】实现文字环绕动态文本效果
前端·javascript·html·harmonyos
程序员小刚2 小时前
基于springboot + vue 的实验室(预约)管理系统
vue.js·spring boot·后端
程序员小刚2 小时前
基于SpringBoot + Vue 的校园论坛系统
vue.js·spring boot·后端
irving同学462383 小时前
Next.js 组件开发最佳实践文档(TypeScript 版)
前端
刺客-Andy3 小时前
React Vue 项开发中组件封装原则及注意事项
前端·vue.js·react.js
marzdata_lily3 小时前
从零到上线!7天搭建高并发体育比分网站全记录(附Java+Vue开源代码)
前端·后端