node.js - systemjs - 映射到不同的 URL

标签 node.js angular systemjs

我是 Angular 2 和 systemjs 的新手,我有一个示例 Node.js 项目,我试图将子文件夹“./client/dashboard”中的 Angular 2 应用程序映射到子 URL“localhost:3000”/dashboard' 使用express.static中间件:

app.use('/dashboard', express.static(__dirname + '/client/dashboard', { maxAge: 86400000 }));

angular 2 app folder structure

我的index.html有以下代码:

<base href="/dashboard">
<link rel="stylesheet" href="dashboard/styles.css">
<script src="dashboard/node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="dashboard/node_modules/systemjs/dist/system.src.js"></script>
<script src="dashboard/node_modules/rxjs/rx.js"></script>
<script src="dashboard/node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="dashboard/node_modules/angular2/bundles/router.dev.js"></script>
<script src="dashboard/node_modules/angular2/bundles/http.dev.js"></script>
<script>
System.config({
    baseURL: '/dashboard',
    // paths: {
    //     '*': 'dashboard/*'
    // },
    map: {
        app: '/dashboard/app',
        rxjs: '/dashboard/node_modules/rxjs',
        angular2: '/dashboard/node_modules/angular2'
    },
    packages: {
        app: { format: 'register', defaultExtension: 'js' },
        rxjs: { defaultExtension: 'js' },
        angular2: { defaultExtension: 'js' }
    }
});
System.import('app/main').then(null, console.error.bind(console));
</script>

我遇到的唯一错误是 rx.ts 上的“require is not Define”和 css 文件上的 404(参见附图 chrome errors )

如果我只是将 Angular 2 应用程序目录映射到“/”,我仍然会在 rx.ts 上收到任意“require is not Defined”错误,但页面加载正确,所以我很确定这不是问题。我看到的唯一的其他错误是 css 错误。有什么想法可能是什么问题吗?蒂亚

最佳答案

也许是因为您包含了两次 rxjs 和 angular2:一次使用脚本元素,另一次在 SystemJS 配置中。

为什么需要在 SystemJS 配置中定义它们?

编辑

这应该足够了:

<base href="/dashboard">
<link rel="stylesheet" href="dashboard/styles.css">
<script src="dashboard/node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="dashboard/node_modules/systemjs/dist/system.src.js"></script>
<script src="dashboard/node_modules/rxjs/rx.js"></script>
<script src="dashboard/node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="dashboard/node_modules/angular2/bundles/router.dev.js"></script>
<script src="dashboard/node_modules/angular2/bundles/http.dev.js"></script>
<script>
  System.config({
    baseURL: '/dashboard',
    map: {
      app: '/dashboard/app'
    },
    packages: {
      app: {
        format: 'register', defaultExtension: 'js'
      }
    }
  });
  System.import('app/main').then(null, console.error.bind(console));
</script>

关于node.js - systemjs - 映射到不同的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35512152/

相关文章:

javascript - SystemJS - ES6 模块加载器 - 导入中的层次结构过多

node.js - Protractor - 出现错误“找不到模块../lib/cli.js

javascript - 如何将文件的每一行传输到数组单元格?

angular - 通过在 typescript 中调用方法打开 bootstrap 4 模式

javascript - Angular:动态呈现时 SVG 路径标记不可见

typescript - 在 HTML 文件中使用 System.js 调用 typescript 类方法

javascript - 将 promise 变量拆分为命名变量

node.js - Nodejs错误: Cannot find module .。在 EC2 中

c# - ASP.Net Core 3.0 JWT Bearer Token 没有可用的 SecurityTokenValidator

angular - 运行时将组件或模块加载到 angular2 中的模块中