项目场景:
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;
# }
#}
}