zoukankan      html  css  js  c++  java
  • 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未找到',
                },
            },


  • 相关阅读:
    Synchronized和Lock的实现原理和锁升级
    如何禁止CPU指令重排
    MESI缓存一致性
    归并排序
    强软弱虚四种引用和ThreadLocal内存泄露
    VINS-Mono代码分析与总结(完整版)
    IMU误差模型与校准
    小感
    K8S conul部署
    Centos Consul集群及Acl配置
  • 原文地址:https://www.cnblogs.com/wuheng1991/p/7609529.html
Copyright © 2011-2022 走看看