Vue

※前提条件:本文基于 Vue 2.0 创作

浏览器刷新404错误 [edit]

原因 [edit]

问题原因我就不说了,官网已经说了,想了解的看这里

https://router.vuejs.org/zh/guide/essentials/history-mode.html#%E5%90%8E%E7%AB%AF%E9%85%8D%E7%BD%AE%E4%BE%8B%E5%AD%90

对策 [edit]

在网上搜了大部分的IIS的解决方案如下:

安装 IIS UrlRewrite [edit]

修改 web.config [edit]

在你的网站根目录中创建一个 web.config(如果已经存在,就不用创建了) 文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Handle History Mode and custom 404/500" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

但是完全按照这个配置后,刷新页面没有问题了,但是请求却不重新获取数据了。

原因 [edit]

添加配置后Cache-Control被改成了private,代表只有页面第一次加载的时候才向服务器请求数据。

解决 [edit]

将匹配的正则表达式<match url="(.*)" /> 改成 <match url="(home)" /> 因为网站每一个页面链接都包含“home”(http://aaa.bbb.com/home/desktop/process),但是请求数据的链接是:http://aaa.bbb.com/api/Account/Login。修改后,配置只对页面链接起做用。 这样刷新后既不报404错误,也能获取到后端数据了。

或者如下:匹配所有不包含api的数据请求

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Handle History Mode and custom 404/500" stopProcessing="true">
          <match url="(^(?!.*?api).*$)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

コメント:



(画像の文字列を入力して下さい)

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS