1.去掉#本地运行没问题很简单:
只需要在你的路由里加上
export default new Router({
mode: ‘history‘, //这句就是让路由转换成history的代码
routes: [
{
path: ‘/‘,
name: ‘demo‘,
component: sy
},
})
只要加上这句,你重启项目就可以没有#号了,但是并没有这么简单,当你上线后会发现页面404。
2.vue去掉#上线.
所以呢,你要在服务端增加一个覆盖所有情况的候选资源:如果 URL 匹配不到任何静态资源,则应该返回同一个 index.html 页面,这个页面就是你 app 依赖的页面。
官网有各种后台配置的说明,这里只把ngnix贴出来。
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#打包后的项目目录,一定记住这个地方带有项目名称
root /Users/a123/Desktop/vue/sgAdmin/yiTownWebApp;
index index.html;
location /yiTownWebApp{
#这个地方没有项目名称,因为请求的时候如果请求:http://localhost:8080/yiTownWebApp,nginx会查找/Users/a123/Desktop/vue/sgAdmin/yiTownWebApp
/目录下的数据
root /Users/a123/Desktop/vue/sgAdmin;
try_files $uri $uri/ @router;
index index.html;
}
//try_files $uri $uri/ @router;和下边部分很重要,没有这部分发布二级一下的路由会出现js加载,但是也没空白的情况
location @router {
rewrite ^.*$ /index.html last;
}
}
后台配置好以后,你以为好了?页面应该会从404,变成空白,这个时候我把需要修改的地方我一起贴出来,具体原因就是路由改了history模式以改变一些路径,不管是路由力度路径或者打包出来的css,js路径都有一些问题,然后你还需要搞清楚你上传的项目是放在服务器根路径还是不是根路径,如果是根路径安装官网的来应该可以解决,如果根路径,看下面。
vue项目中config文件下index.js中打包配置
build: {
// Template for index.html
index: path.resolve(__dirname, ‘../yiTownWebApp/index.html‘),
// Paths
assetsRoot: path.resolve(__dirname, ‘../yiTownWebApp‘), //这个是打包后的文件夹
assetsSubDirectory: ‘static‘, //这个是设置打包后文件夹的路径
assetsPublicPath: ‘/yiTownWebApp/‘,//这个地方使用绝对路径很重要
/**
* Source Maps
*/
productionSourceMap: true,
// https://webpack.js.org/configuration/devtool/#production
devtool: ‘#source-map‘,
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: [‘js‘, ‘css‘],
最后在你的路由里配置一个base
export default new Router({
mode: ‘history‘, //这句就是让路由转换成history的代码
base: ‘/yiTownWebApp/‘, //这个配置也很重要,否则会出现页面空白情况
routes: [
{
path: ‘/‘,
name: ‘demo‘,
component: demo
},
})
现在就可以访问了,但是用了嵌套路由的还是会发现子页面还是空白,所以主页面也记得加base
{
path: ‘/2018jbGame‘,
name: ‘jbGame2018‘,
component: jbGame2018,
base: ‘/2018jbGame/‘,
meta: {
title: ‘江北区第八届运动会‘
},
children: [
{
path: ‘/indexPage‘,
component: IndexPage,
name: ‘IndexPage‘,
meta: {
title: ‘江北区第八届运动会‘
},
},
}
原文:https://www.cnblogs.com/huhanhaha/p/9473699.html