【问题标题】:How do I implement systemjs in Angular 6+?如何在 Angular 6+ 中实现 systemjs?
【发布时间】:2019-05-03 00:22:43
【问题描述】:

我正在尝试学习如何使插件在 Angular 中工作,但是在添加 systemjs 后出现以下错误:

Uncaught ReferenceError: SystemJS is not defined

我是这样实现 systemjs 的:

import { System } from 'systemjs';
declare const SystemJS: System;

import * as angularCore from '@angular/core';
import * as angularCommon from '@angular/common';
import * as angularCommonHttp from '@angular/common/http';
import * as angularForms from '@angular/forms';
import * as angularAnimations from '@angular/animations';
import * as angularPlatformBrowser from '@angular/platform-browser';
import * as angularPlatformBrowserDynamic from '@angular/platform-browser-dynamic';

//error thrown here
SystemJS.set('@angular/core', SystemJS.newModule(angularCore));
SystemJS.set('@angular/common', SystemJS.newModule(angularCommon));
SystemJS.set('@angular/common/http', SystemJS.newModule(angularCommonHttp));
SystemJS.set('@angular/forms', SystemJS.newModule(angularForms));
SystemJS.set('@angular/animations', SystemJS.newModule(angularAnimations));
SystemJS.set('@angular/platform-browser', SystemJS.newModule(angularPlatformBrowser));
SystemJS.set('@angular/platform-browser-dynamic', SystemJS.newModule(angularPlatformBrowserDynamic));

SystemJS.config({ meta: { '*': { authorization: true } } });

我错过了什么?

我使用 systemjs 的代码:https://github.com/FrisoDenijs/angular-pluggable-architecture/blob/master/dashboard/src/app/dashboard/dashboard/dashboard.component.ts

我尝试修复的提交:https://github.com/FrisoDenijs/pluggable-angular-example/commit/4472560da17b69c13809be931f6966d9254d10d1

我使用的仓库:https://github.com/paucls/angular-pluggable-architecture

【问题讨论】:

  • 尝试在您的/src/polyfill.ts 文件中导入systemjs。我们使用在以前版本的 Angular 中导入 SystemJS,但最新版本在一些 Angular 模块中具有 systemJs。
  • @AbdulSamiHaroon 我尝试像 import 'systemjs';import '../node_modules/systemjs'; 这样添加到 polyfill,但我收到以下错误“找不到模块:错误:无法解析 [.. .]”。我还应该怎么做才能将它添加到 polyfill 中?
  • // 将全局添加到窗口,分配窗口本身的值。 (window as any).global = window; 我在我的一个项目中添加了这个来解决这个错误。我不知道,但请尝试将其添加到您的 polyfill.ts 的底部。
  • @AbdulSamiHaroon 我认为我做错了,它仍然无法正常工作。你能看看吗? github.com/FrisoDenijs/pluggable-angular-example/commit/…

标签: angular angular6 systemjs


【解决方案1】:
  1. 通过您的package.json 安装 SystemJS(也许您需要安装旧版本以满足您的需要)。

  2. 将 SystemJS Javascript 文件的路径添加到 angular.json 内的脚本中。这样它就会被加载到全局空间中:

"scripts": [
  "node_modules/systemjs/dist/system.min.js"
]
  1. 在您的文件中声明并使用它。
...
declare const SystemJS: System;
...
SystemJS.set('@angular/core', SystemJS.newModule(angularCore));

第 2 步是您所缺少的(我猜),也是它在您链接的存储库中起作用的原因;以同样的方式完成:https://github.com/paucls/angular-pluggable-architecture/blob/master/dashboard/.angular-cli.json

您的代码实际上并没有导入 SystemJS 模块本身,它只是导入了在运行时被剥离的类型声明。这就是为什么您需要将 SystemJS 添加到 scripts-section

【讨论】:

猜你喜欢
  • 2019-05-10
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
相关资源
最近更新 更多