如何正确刷新在VS 2015 Web开发环境中运行的AngularJS SPA

前端之家收集整理的这篇文章主要介绍了如何正确刷新在VS 2015 Web开发环境中运行的AngularJS SPA前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个AngularJS SPA应用程序,我使用Visual Studio 2015开发.当我点击发布它发布index.html并且工作得很好.但是,如果我在页面上并单击刷新,则会尝试刷新SPA页面,例如example.com/home/about.这失败了,因为我没有家/关于页面.

有没有办法可以修改我的web.config文件(仅用于本地测试),以便它实际上转到index.html(加载它)然后转到/ home / about状态?

这是我当前的web.config:

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
</configuration>

解决方法

您似乎正在使用 html5mode.在这种情况下,没有#来保持URL从请求更改为服务器.
使用此配置,您需要来自服务器的帮助.当它收到来自SPA路线的请求时,它将为index.html提供服务.

这个SO answer有关于在web.config上配置URL Rewrite的详细信息:

规则通过:

<system.webServer>
    <rewrite>
      <rules>
        <rule name="AngularJS Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

它假定您的API位于:/ api下,并且对于它找到的任何目录或文件,它按原样使用.
任何其他的,被重写为/将默认文档配置为index.html,将加载SPA.

另请注意,您需要安装URL Rewrite module for IIS(IIS Express doesn’t need the module)

另一个选项是这些轻量级HTTP服务器npm包之一.
John Papa has one: lite-server.它在引擎盖下使用BrowserSync

BrowserSync does most of what we want in a super fast lightweight
development server. It serves the static content,detects changes,
refreshes the browser,and offers many customizations.

When creating a SPA there are routes that are only known to the
browser. For example,/customer/21 may be a client side route for an
Angular app. If this route is entered manually or linked to directly
as the entry point of the Angular app (aka a deep link) the static
server will receive the request,because Angular is not loaded yet.
The server will not find a match for the route and thus return a 404.
The desired behavior in this case is to return the index.html (or
whatever starting page of the app we have defined). BrowserSync does
not automatically allow for a fallback page. But it does allow for
custom middleware. This is where lite-server steps in.

lite-server is a simple customized wrapper around BrowserSync to make it easy to serve SPAs.

原文链接:https://www.f2er.com/html/227283.html

猜你在找的HTML相关文章