系统配置 js Angular 2 安装模块的问题.解释



我最近开始使用 Angular 2 进行开发。那里有很多教程。但我仍然无法理解的一件事是systemjs.config.js.

我了解为什么使用它,以及它的主要目的是什么。但是如何使用它来添加其他依赖项还不够清楚。

我正在使用入门存储库中的默认包。 但正如我所提到的,我在添加新模块时遇到了麻烦。

我已经尝试了几次包含不同的模块,但经过几次重试后我放弃了。

我试图找到一个好的教程或文档来解释如何在 Angular 2 的上下文中处理此工具,但没有结果。

目前我无法安装Material Design Lite模块。

这是我systemjs.config.js文件

(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
'@angular-mdl/core': 'vendor/@angular-mdl/core',
// angular bundles
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'@angular/material': 'npm:@angular/material/bundles/material.umd.js',
// other libraries
'hammerjs': 'npm:hammerjs/hammer.js',
'rxjs': 'npm:rxjs',
'angular2-mdl': 'vendor/angular2-mdl',
'primeng': 'npm:primeng'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
defaultExtension: 'js'
},
'angular2-mdl': {main: 'bundle/angular2-mdl.js'},
rxjs: {
defaultExtension: 'js'
},
primeng: {
defaultExtension: 'js'
}
}
});
})(this);

我的index.html

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic">
<link rel="stylesheet" href="/node_modules/material-design-lite/material.min.css">
<script src="/node_modules/material-design-lite/material.min.js"></script>
<link rel="stylesheet" href="/node_modules/material-design-icons/iconfont/material-icons.css">    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="node_modules/@angular/material/core/theming/prebuilt/deeppurple-amber.css" rel="stylesheet">
<link rel="stylesheet" href="bootstrap.min.css">
<link rel="stylesheet" href="style.css">

但是我仍然收到以下错误。

模板解析错误:"mdl-icon"不是已知元素:1. "mdl-icon"是一个 Angular 组件,然后验证它是否是 此模块。2. 如果"mdl 图标">

请解释如何使用此文件。或者至少如何包含Material Design Lite模块。

">

供应商"是一个通用术语,用于描述提供软件包的地方,因此它可能是某种形式的CDN,或者在您的情况下是npm。

首先要做的是用npm:替换vendor/,这样它将在项目的node_modules文件夹中查找,而不是很可能不存在的名为 vendor 的文件夹

仍然不知道所有这些东西是如何工作的。但是要使其正常工作,请执行以下操作。

(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
'angular2-mdl': 'npm:angular2-mdl/',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
'angular2-mdl': {main: 'bundle/angular2-mdl.js'},
}
});
})(this);

并且不要忘记在您的app.module.js中导入MdlModule

@NgModule({
imports: [BrowserModule, MdlModule, HttpModule, MaterialModule, RoutingModule],

最新更新