zoukankan      html  css  js  c++  java
  • Vue中的Cannot GET / xxx

    场景

    • Vue2.6.12

    • 模式:单页面应用(SPA)模式

    • 路由模式:history 模式

    【问题一】 刷新页面,出现 Cannot GET / xxx

    【解决】 设置 historyApiFallback: true

    webpack.config.js

    devServer: {
      ...
      historyApiFallback: true
      ...
    }
    

    【问题二】在问题一解决的基础上增加代理(proxy)配置,刷新页面,再次出现 Cannot GET / xxx问题,并且控制台还有报错信息

    • 配置

    webpack.config.js

    devServer: {
      ...
      historyApiFallback: true,
      proxy: {
        '/': {
          target: 'http://localhost:3000',
        }
      },
      ...
    }
    
    • 问题

    【解决】 设置 bypass

    webpack.config.js

    devServer: {
      ...
      historyApiFallback: true,
      proxy: {
        '/': {
          target: 'http://localhost:3000',
          bypass: function (req, res, proxyOptions) {
            if (req.headers.accept.indexOf('html') !== -1) {
              return 'index.html'; // SPA,只有一个html页面,所有都返回index.html页面(个人理解)
            }
          },
        }
      },
      ...
    }
    

    HtmlWebpackPlugin插件的配置

    • 输出的文件:index.html,所以上面 bypass 返回的是 index.html
    new HtmlWebpackPlugin({
      title: 'Scaffolds',
      template: './public/index.ejs', // 指定模板html文件
      filename: 'index.html', // 输出的html文件名称
      favicon: './public/favicon.ico',
      hash: true,
      cache: true,
    }),
    
  • 相关阅读:
    MTK 官方 openwrt SDK 使用
    PF_RING packet overwrites
    pycares cffi
    libevent evbuffer bug
    浮点转字符串性能比较
    重写 libev 的 EV_WIN32_HANDLE_TO_FD
    thrift TNonblockingServer 使用
    accel-pptp 部署
    boost::asio 使用 libcurl
    蜂鸟A20开发板刷 cubietruck 的 SD 卡固件
  • 原文地址:https://www.cnblogs.com/uakora/p/14186659.html
Copyright © 2011-2022 走看看