lite-server 不从 Angular 2 应用程序中的父目录加载脚本

lite-server does not load scripts from parent directory in Angular 2 app

我正在尝试 运行 我的应用程序使用 lite-server 节点包,但它不会从父目录加载脚本。

我想将我的 index.html 放在 /src 文件夹中,因为将来可能会生成与 /dist 不同的文件。 /node_modulessystemjs.config.js 需要保留在根目录中,因为它们不会更改。

文件结构:

src/index.html:

<html>
    <head>
        <base href="/">
        <title>task manager</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="styles.css">
        <!-- 1. Load libraries -->
        <!-- Polyfill(s) for older browsers -->
        <script src="../node_modules/core-js/client/shim.min.js"></script>
        <script src="../node_modules/zone.js/dist/zone.js"></script>
        <script src="../node_modules/reflect-metadata/Reflect.js"></script>
        <script src="../node_modules/systemjs/dist/system.src.js"></script>
        <!-- 2. Configure SystemJS -->
        <script src="../systemjs.config.js"></script>
        <script>
            System.import('app').catch(function(err){ console.error(err); });
        </script>
    </head>
    <!-- 3. Display the application -->
    <body>
        <my-app>Loading...</my-app>
    </body>
</html>

运行 /src 目录中的精简服务器:

节点模块确实存在。如果我将所需文件移动到子目录本身 /src,服务器 运行 没有 404 错误。这是我的 lite-server 或 systemjs 设置的问题吗?

您需要正确配置lite-server

在项目根目录中,创建一个名为 bs-config.json 的文件,内容如下:

{
  "port": 8000,
  "server": {
    "baseDir": "./src",
    "routes": { "/": "./" }
  }
}

现在您必须 运行 lite-server 在与 bs-config.json 文件相同的目录中。

如果您想了解更多关于 lite-server 配置的信息,请阅读 Browsersync Options 文档。

此外,使用此配置,您可以在 index.html 文件中更改脚本的路径:

    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>