vue history模式在nginx下错误的解决方案

原始链接:http://tigerliu.site/2018/10/vue-history/

众所周知 vue 默认的路由为 hash 模式,在 URL 上面会带一个#号,然而某些时候并不是我们想要的。如何去掉#号?去掉#号后的一些问题如何处理?下面我们简单探讨下这块内容。
使用 history 模式
如果不想要很丑的 hash,我们可以用路由的 history 模式,这种模式充分利用 history.pushState API 来完成 URL 跳转而无须重新加载页面。

修改路由模式
const router = new VueRouter({
mode: ‘history’,
routes: […]
})

修改 config/index.js
config/index.js 中 build 下的 assetsPublicPath 从’./‘改成’/‘

module.exports = {
build: {
env: require(’./prod.env’),
index: path.resolve(__dirname, ‘…/dist/index.html’),
assetsRoot: path.resolve(__dirname, ‘…/dist’),
assetsSubDirectory: ‘static’,
assetsPublicPath: ‘/’,
productionSourceMap: true,
… 其他代码略

在 nginx 下的配置Apache

nginx

location / {
try_files $uri $uri/ /index.html;
}
原生 Node.js

const http = require(‘http’);
const fs = require(‘fs’);
const httpPort = 80;

http
.createServer((req, res) => {
fs.readFile(‘index.htm’, ‘utf-8’, (err, content) => {
if (err) {
console.log(‘We cannot open “index.htm” file.’);
}

  res.writeHead(200, {
    'Content-Type': 'text/html; charset=utf-8'
  });

  res.end(content);
});

})
.listen(httpPort, () => {
console.log(‘Server listening on: http://localhost:%s’, httpPort);
});
详细配置见官方配置

404 的解决方案
第一种方式见上面–在 nginx 下的配置,还是贴一下:

location / {
try_files $uri $uri/ /index.html;
}
第二招,逻辑类似

location / {
root /data/nginx/html;
index index.html index.htm;
if (!-e $request_filename) {
rewrite ^/(.*) /index.html last;
break;
}
}
第三式,直接转发到 index.html

location / {
root /data/nginx/html;
index index.html index.htm;
error_page 404 /index.html;
}
第四招,类似第二招

location / {
root html;
try_files $uri KaTeX parse error: Expected 'EOF', got '}' at position 48: …tml index.htm; }̲ location @rout… /index.html last;
}
Uncaught SyntaxError: Unexpected token <错误
配置好了刷新后一片空白?打开控制台一看什么鬼,Uncaught SyntaxError: Unexpected token <错误?

貌似是 JS 的错误,下面我们逐一排查。

错误排查
检查配置有没有问题:

检查 config/index.js 中 build 下的 assetsPublicPath 是否正确?

module.exports = {
build: {
env: require(’./prod.env’),
index: path.resolve(__dirname, ‘…/dist/index.html’),
assetsRoot: path.resolve(__dirname, ‘…/dist’),
assetsSubDirectory: ‘static’,
assetsPublicPath: ‘/’,
productionSourceMap: true,
… 其他代码略
检查模式是否为 history

const router = new VueRouter({
mode: ‘history’,
routes: […]
})
检查路由路径
你的子路径需要加上去,给每一个 component 加上 name,例如:

const router = new Router({
mode: ‘history’,
routes: [
{
path: ‘/’,
component: Index,
name: ‘index’,
redirect: ‘/home’,
children: [{
path: ‘home’, //这里会不会有问题?
component: Home
},
{
path: ‘goodsDetails’, //子路径需要配完整,应该是/home/goodsDetails
component: goodsDetails
},
}
]
})
以上都检查完,打包重新部署看看

修复问题
如果还有问题,试试下面的配置,其实 js、css、png 文件我们不需要转到 index.html

location / {
root html;
rewrite ^.+(?<!js|css|png|map)$ /index.html break;
index index.html index.htm;
}
结语
遇到问题,先分析下具体的原因,一步一步排查,总归还是有解决办法的。出现 404,空白页,JS 问题,总觉得是我们前端哪儿有问题,排查完才发现是配置问题,以上几招供大家参考,能解决问题的方法就是好方法!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 将Vue Router的history模式部署在Nginx下需要进行一些配置。主要包括以下几步: 1. 在Nginx配置文件中,添加一条location规则,将所有匹配到的请求都指向index.html文件。 2. 配置Vue Router的base路径,即Vue.js开发环境中在router/index.js文件中的base选项,设为Nginx服务的根路径(例如:/project-name/)。 3. 配置Nginx的try_files选项,用于重定向请求到正确的URL。具体方式为将Nginx的try_files选项与Vue Router的fallback选项进行关联。 以上是实现Vue Router history模式Nginx下部署的主要步骤。需要注意的是,对于不同版本的NginxVue Router,具体的配置方式可能会有所不同。 ### 回答2: vue router是vue.js的官方路由管理器,是一个非常流行的单页面应用程序(SPA)路由方案。而history模式vue router的一种模式,它使用HTML5 history API将URL映射到应用程序的状态,而不是使用hash来实现路由。使用这种模式可以使URL更加整洁,更易于理解和管理。在nginx下部署配置vue router history模式,有以下几个步骤: 1. 在vue项目中设置history模式: 在vue项目中的router文件中,配置路由模式history。在vue-cli创建的脚手架中已经默认配置为history模式。 ```javascript const router = new VueRouter({ mode: 'history', routes }) ``` 2. 配置nginx服务器 在nginx服务器上,需要添加一个location来匹配vue路由中的所有URL,以保证页面路由能够正确渲染。配置文件中需要添加以下代码: ```nginx location / { try_files $uri $uri/ /index.html; } ``` 其中$uri指当前请求的URI路径,$uri/指当前URI路径下的子文件夹,index.html是我们设置的vue项目的入口文件。 3. 修改nginx配置文件 在nginx的配置文件中,需要将单页应用所需的页面路由指向正确目录下的静态文件。只有这样,浏览器才会正确显示页面的内容。如果vue项目的build目录下存在静态文件,则需要将静态文件目录和路由路径指向该目录。例如: ```nginx server { listen 80; server_name example.com; root /var/www/example.com; index index.html; location / { try_files $uri $uri/ /index.html; } } ``` 在这个例子中,vue项目被放置在 /var/www/example.com 目录下。当请求 example.com/about 时,nginx会将请求转发到index.html文件,然后vue.router将根据URL的路径加载对应的组件,最终呈现出关于页面。 总之,在Nginx下部署Vue Router history模式需要在vue的router中配置history模式并在nginx服务器上添加location来匹配vue路由中的所有URL,确保能够正确渲染页面。同时nginx需要将请求转发到index.html文件,并将vue项目放置在/static/目录下。 ### 回答3: Vue Router是Vue.js专用的官方路由,支持hash模式history模式。其中history模式不需要“#”符号在URL上,因此被认为是更加符合SEO要求的模式。在Nginx中部署这种模式需要以下步骤: 1. 配置Nginx 首先,需要配置Nginx,将所有的URL请求不直接匹配到文件,而是转发至index.html文件。 例如: ``` location / { try_files $uri $uri/ /index.html; } ``` 这段配置指定当请求不是一个文件或目录时,将请求传递至index.html文件。 2. 配置Vue Router 其次,在Vue Router配置文件中,需要设置base属性,该属性指定所有URL路径的公共路径,即Nginx中的根路径。 例如: ``` const router = new VueRouter({ mode: 'history', base: '/vue-router-demo/', routes: [...] }) ``` 这里的base属性设置为'/vue-router-demo/', 意味着所有路由路径都跟"http://yourdomain.com/vue-router-demo/" URL开头。这个路径应该与Nginx中的根路径相匹配。 3. 根据实际情况调整Nginx配置。 最后,需要根据实际情况对Nginx配置进行调整。这可能涉及到解决跨域问题、安全问题、性能优化等问题。 综上所述,Vue Router的history模式Nginx下需要特殊配置,但一旦配置完成,将提供更好的用户体验和SEO效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值