我有一些具体问题。我是角度2的新手,所以我认为解决方案非常简单。
所以,我有自己之前创建的自定义标记。我在其中添加了一些组件,指令和服务。我的主要组件的名称是“reddit”。有两个标签和输入的位置形式。 当我在index.html中添加一个reddit标记实例时 - 一切正常。 但是,只要我加倍,就像这样:
<reddit>Loading...</reddit>
<reddit>Loading</reddit>
加载并显示。第二个仅显示“正在加载”消息。我认为我的配置文件中的问题。 你能看一下并帮助我吗? THX!
我的package.json:
{
"name": "the-basics-of-angular-2",
"version": "1.0.0",
"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"lite": "lite-server",
"tsc": "tsc",
"tsc:w": "tsc -w",
"go": "concurrent \"npm run tsc:w\" \"npm run lite\" "
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/angular/angular.io/blob/master/LICENSE"
}
],
"dependencies": {
"@angular/common": "~2.1.1",
"@angular/compiler": "~2.1.1",
"@angular/core": "~2.1.1",
"@angular/forms": "~2.1.1",
"@angular/http": "~2.1.1",
"@angular/platform-browser": "~2.1.1",
"@angular/platform-browser-dynamic": "~2.1.1",
"@angular/router": "~3.1.1",
"@angular/upgrade": "~2.1.1",
"angular-in-memory-web-api": "~0.1.13",
"core-js": "^2.4.1",
"reflect-metadata": "^0.1.8",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.39",
"zone.js": "^0.6.25"
},
"devDependencies": {
"@types/core-js": "^0.9.34",
"@types/node": "^6.0.45",
"concurrently": "^3.0.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.3"
}
}
我的system.config.js
/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(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 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/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
}
}
});
})(this);
我的tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
}
}
我的自定义组件的代码:
import {Component, ViewChild} from '@angular/core';
import {Link} from "../list/link";
import {ComponentList} from "../list/component-list.component";
import {LinkService} from "../service/link.service";
import {Logger} from "../service/logger.service";
@Component({
selector: 'reddit',
moduleId: module.id,
styleUrls: ['reddit.css'],
templateUrl: "component.html",
providers: [LinkService, Logger]
})
export class RedditAppComponent{
@ViewChild(ComponentList)
private child : ComponentList;
constructor(private linkService : LinkService){
}
title : string = "";
link : string = "";
linkList : Link[] = new Array<Link>();
addArticle(title : string, link : string){
this.linkService.addData(new Link(title, link));
this.title = "";
this.link = "";
this.child.childMethod();
return false;
}
deleteArticle(){
this.linkService.deleteData();
return false;
}
ngOnInit(){
this.linkList = this.linkService.getData();
}
}
自定义组件的html架构:
<form class="ui large form segment" #form="ngForm">
<h3 class="ui header">Add new row</h3>
<div class="field">
<label>Header:</label>
<input type="text" [(ngModel)]="title" name="title">
</div>
<div class="field">
<label>Link:</label>
<input type="text" [(ngModel)]="link" name="link">
</div>
<button class="ui positive right floated button" (click)="addArticle(title, link)">
Send
</button>
<button class="ui positive right floated button" (click)="deleteArticle()">
Delete
</button>
</form>
<list-components [links] = "linkList"></list-components>