加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

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

发布时间:2020-12-14 16:36:09 所属栏目:资源 来源:网络整理
导读:我有一个AngularJS SPA应用程序,我使用Visual Studio 2015开发.当我点击发布它发布index.html并且工作得很好.但是,如果我在页面上并单击刷新,则会尝试刷新SPA页面,例如example.com/home/about.这失败了,因为我没有家/关于页面. 有没有办法可以修改我的web.co
我有一个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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读