简体   繁体   English

使用相对网址绕过角度2路由

[英]Bypass angular 2 routing with a relative url

I have googled a lot but cannot find a solution on this problem. 我已经用谷歌搜索了很多,但是找不到这个问题的解决方案。 I have a relative path like 'external/user/login/example.php' and I need in that case to bypass the angular routing to get my_env + relative_url. 我有一个相对路径,例如“ external / user / login / example.php”,在这种情况下,我需要绕过角度路由以获取my_env + relative_url。

Is that possible with angular? 用角度可以吗? Thanks in advance. 提前致谢。

Well, if you navigate to that URL with RouterLink or one of the navigate methods, no. 好吧,如果您使用RouterLink或其中一种navigate方法导航到该URL,则不会。 You just navigate directly using the browser API, bypassing Angular: 您只需绕过Angular,直接使用浏览器API进行导航:

window.location = ' /external/user/example.php';

Of course, you must make sure this route does not send you to the html document with the hosted Angular application. 当然,您必须确保此路由不会将您发送到带有托管Angular应用程序的html文档。 You might have to check your server's routing configuration for this. 为此,您可能必须检查服务器的路由配置。

You can try this, in your Angular Web.config file 您可以在Angular Web.config文件中尝试此操作

<?xml version="1.0" encoding="utf-8"?>
<configuration>

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Angular Routes" stopProcessing="true">
        <match url="^bulletinboardapi/?" negate="true"/>
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />      
        </conditions>
        <action type="Rewrite" url="/index.html" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

</configuration>

Explanation 说明

<match url="^bypassPath/?" negate="true"/> tag tells angular that if URL comes without /bypassPath/ then route as per angular.

www.yourwebsite.com/bypassPath/api/user/get will go to your API path (Server). In many cases bypassPath may be your virtual application pointing to your API project.

www.yourwebsite.com/login/ will route as per angular even if you refresh browser.
www.yourwebsite.com/home/ will route as per angular even if you refresh browser.

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM