服务器之家:专注于服务器技术及软件下载分享
分类导航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服务器之家 - 编程语言 - JavaScript - VUE : vue-cli中去掉路由中的井号#操作

VUE : vue-cli中去掉路由中的井号#操作

2021-09-15 16:44歌歌的前端学习之路 JavaScript

这篇文章主要介绍了VUE : vue-cli中去掉路由中的井号#操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

vue-cli项目中,如果想去掉url地址栏中的“#”我们可以用路由的 history 模式,这种模式充分利用 history.pushState API 来完成 URL 跳转而无须重新加载页面。

只需要在路由表中,加入一行代码即可。

VUE : vue-cli中去掉路由中的井号#操作

VUE : vue-cli中去掉路由中的井号#操作

补充知识:vue 打包部署去掉#注意事项

示例基于vue cli2.0脚手架生成的项目

1.vue项目中config文件下index.js中打包配置:

  1. build: {
  2. // Template for index.html
  3. index: path.resolve(__dirname, '../dist/index.html'),
  4.  
  5. // Paths
  6. assetsRoot: path.resolve(__dirname, '../dist'),
  7. assetsSubDirectory: 'static',
  8. assetsPublicPath: '/dist/', // 这个地方使用绝对路径 很重要!
  9.  
  10. /**
  11. * Source Maps
  12. */
  13.  
  14. productionSourceMap: true,
  15. // https://webpack.js.org/configuration/devtool/#production
  16. devtool: '#source-map',
  17.  
  18. // Gzip off by default as many popular static hosts such as
  19. // Surge or Netlify already gzip all static assets for you.
  20. // Before setting to `true`, make sure to:
  21. // npm install --save-dev compression-webpack-plugin
  22. productionGzip: false,
  23. productionGzipExtensions: ['js', 'css'],
  24.  
  25. // Run the build command with an extra argument to
  26. // View the bundle analyzer report after build finishes:
  27. // `npm run build --report`
  28. // Set to `true` or `false` to always turn it on or off
  29. bundleAnalyzerReport: process.env.npm_config_report
  30. }

2.路由配置:router文件夹下index.js:

  1. export default new Router({
  2. mode: 'history', // 去掉#,需要路由模式改为history
  3. base: '/dist/', // 这个配置也很重要,否则会出现页面空白情况
  4. scrollBehavior: () => ({ y: 0 }),
  5. routes: [
  6. {
  7. path: '/facebook',
  8. name: 'Facebook',
  9. component: Facebook
  10. },
  11. {
  12. path: '/google',
  13. name: 'Google',
  14. component: Google
  15. }
  16. ]
  17. })

3.剩下的不懂 就要找你得运维或者后端开发小伙伴了,nginx配置:

  1. server {
  2. listen 8080;
  3. server_name localhost;
  4. #charset koi8-r;
  5. #access_log logs/host.access.log main;
  6. #打包后的项目目录,一定记住这个地方带有项目名称
  7. root /Users/qiilee/Desktop/vue/dist;
  8. index index.html;
  9.  
  10. location /dist {
  11. #这个地方没有项目名称,因为请求的时候如果请求:http://localhost:8080/dist ,nginx会查找/Users/qiilee/Desktop/vue/dist/目录下的数据
  12. root /Users/qiilee/Desktop/vue;
  13. try_files $uri $uri/ @router;
  14. index index.html;
  15. }
  16. //try_files $uri $uri/ @router;和下边部分很重要,没有这部分发布二级一下的路由会出现js加载,但是也没空白的情况
  17. location @router {
  18. rewrite ^.*$ /index.html last;
  19. }
  20. }

以上这篇VUE : vue-cli中去掉路由中的井号#操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/webEvelement/article/details/82850551

延伸 · 阅读

精彩推荐