• Vue 404页面处理


    问题原因:

    刷新页面时访问的资源在服务端找不到,因为vue-router设置的路径不是真实存在的路径

    解决方案:

    第一步:后端配置

    Apache

    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
      RewriteRule ^index.html$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.html [L]
    </IfModule>
    

    nginx

    location / {
      try_files $uri $uri/ /index.html;
    }
    

    Native 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)
    })

    第二步:前端配置
    const router = new VueRouter({
      mode: 'history',
      routes: [
        { path: '*', component: NotFoundComponent }
      ]
    })
    

    -------------------------------------------------------------------------------------------------
    如:
       // 404未找到
            {
                path: '*',
                component: notFind,
                meta: {
                    title: '404未找到',
                },
            },


  • 相关阅读:
    prufer 序列 学习笔记
    题解 [SDOI2009]E&D/染色游戏/Moving Pebbles
    题解 Christmas Game
    题解 [HNOI/AHOI2018]毒瘤
    题解 UVA1500 Alice and Bob
    Mac端影片压制流程
    react:Text nodes cannot appear as a child
    阿里云docker镜像地址
    Vscode 智能插件
    vue-vant实现IndexBar索引栏
  • 原文地址:https://www.cnblogs.com/wuheng1991/p/7609529.html
Copyright © 2020-2023  润新知