未捕获到的SyntaxError:inline.bundle.js:1中出现意外的令牌<-Angular v4

Hoo*_*n L 5 javascript deployment iis unexpected-token angular

部署到IIS后,我发现我的路由均不起作用,因此我进行了研究,然后发现了这个问题,其中说您必须向进行重写web.config,而我已经做了,路由现在可以正常工作。

这是我的一些在开发模式下有效但在生产中有效的路线:

const appRoutes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'manage', component: ManageComponent },
  { path: 'manage/products', component: ProductComponent },
  { path: 'manage/products/:action/:id', component: ProductComponent },
  { path: 'manage/companies', component: CompanyComponent },
];  
Run Code Online (Sandbox Code Playgroud)

我所做的web.config

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Angular 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>
Run Code Online (Sandbox Code Playgroud)

那么实际的问题是什么呢?当我刷新/重定向页面时会出现问题。经过一个小时的研究,发现我编写的规则web.config总是返回index.html,这就是为什么我得到Uncaught SyntaxError: Unexpected token <以下文件的原因:

inline.bundle.js:1
polyfills.bundle.js:1
styles.bundle.js:1
vendor.bundle.js:1
main.bundle.js:1
Run Code Online (Sandbox Code Playgroud)

您建议如何解决此问题?

更新:通过manage从路由中删除,解决了不带参数的路由的问题,但是对于包含参数的路由,问题仍然存在。

RMu*_*esi 5

有同样的问题。通过将 index.html 中的基本 href 标记更改为完全限定的基本 url 来修复。

前:

<!doctype html>
<html lang="en">

  <head>
    <meta charset="utf-8">
    <title>MySubApp</title>
    <base href="/">

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" type="image/x-icon" href="favicon.ico">


  </head>

  <body>
    <mu-root></mu-root>

  <script type="text/javascript" src="inline.bundle.js"></script><script type="text/javascript" src="polyfills.bundle.js"></script><script type="text/javascript" src="scripts.bundle.js"></script><script type="text/javascript" src="styles.bundle.js"></script><script type="text/javascript" src="vendor.bundle.js"></script><script type="text/javascript" src="main.bundle.js"></script></body>

</html>
Run Code Online (Sandbox Code Playgroud)

后:

<!doctype html>
<html lang="en">

  <head>
    <meta charset="utf-8">
    <title>MySubApp</title>
    <base href="http://MyUrl.com/MySubApp/">

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" type="image/x-icon" href="favicon.ico">


  </head>

  <body>
    <mu-root></mu-root>

  <script type="text/javascript" src="inline.bundle.js"></script><script type="text/javascript" src="polyfills.bundle.js"></script><script type="text/javascript" src="scripts.bundle.js"></script><script type="text/javascript" src="styles.bundle.js"></script><script type="text/javascript" src="vendor.bundle.js"></script><script type="text/javascript" src="main.bundle.js"></script></body>

</html>
Run Code Online (Sandbox Code Playgroud)

不是 100% 清楚到底为什么,但 IIS 重写 url 的方式似乎改变了基本根的传递方式,这破坏了相对路径“/”base-href。