我在编写一个简单的reddit克隆应用程序时遵循ng-book2 . 到目前为止,我所拥有的只是一个index.html文件和一个app.ts文件 .
现在在我的app.ts文件中,我声明了2个组件 . 第二个组件 reddit
应该加载第一个组件 - reddit-article
和 Article
类 . 现在运行服务器时出现以下错误:
angular2.dev.js:23501 EXCEPTION:在ArticleComponentBrowserDomAdapter.logError @ angular2.dev.js上找不到指令注释:23501BrowserDomAdapter.logGroup @ angular2.dev.js:23512ExceptionHandler.call @ angular2.dev.js:1185(匿名函数) @ angular2.dev.js:12489NgZone._notifyOnError @ angular2.dev.js:13533collection_1.StringMapWrapper.merge.onError @ angular2.dev.js:13437Zone.run @ angular2-polyfills.js:1247(匿名函数)@ angular2.dev .js:13456zoneBoundFn @ angular2-polyfills.js:1220lib $ es6 $ promise $$ internal $$ tryCatch @ angular2-polyfills.js:468lib $ es6 $ promise $$ internal $$ invokeCallback @ angular2-polyfills.js:480lib $ es6 $ promise $$ internal $$ publish @ angular2-polyfills.js:451(匿名函数)@ angular2-polyfills.js:123microtask @ angular2.dev.js:13488Zone.run @ angular2-polyfills.js:1243(匿名函数) @ angular2.dev.js:13456zoneBoundFn @ angular2-polyfills.js:1220lib $ es6 $ promise $ asap $$ flush @ angular2-polyfills.js:262 angular2.dev.js:23501 STACKTRACE:BrowserDom Adapter.logError @ angular2.dev.js:23501ExceptionHandler.call @ angular2.dev.js:1187(匿名函数)@ angular2.dev.js:12489NgZone._notifyOnError @ angular2.dev.js:13533collection_1.StringMapWrapper.merge.onError @ angular2.dev.js:13437Zone.run @ angular2-polyfills.js:1247(匿名函数)@ angular2.dev.js:13456zoneBoundFn @ angular2-polyfills.js:1220lib $ es6 $ promise $$ internal $$ tryCatch @ angular2- polyfills.js:468lib $ es6 $ promise $$ internal $$ invokeCallback @ angular2-polyfills.js:480lib $ es6 $ promise $$ internal $$ publish @ angular2-polyfills.js:451(匿名函数)@ angular2-polyfills . js:123microtask @ angular2.dev.js:13488Zone.run @ angular2-polyfills.js:1243(匿名函数)@ angular2.dev.js:13456zoneBoundFn @ angular2-polyfills.js:1220lib $ es6 $ promise $ asap $$ flush @ angular2-polyfills.js:262 angular2.dev.js:23501错误:在新的BaseException上没有在ArticleComponent上找到指令注释(http:// localhost:8080 / node_modules / angular2 / bundles / angular2.dev.js:7385:21 )在Direct iveResolver.resolve(http:// localhost:8080 / node_modules / angular2 / bundles / angular2.dev.js:6688:13)at RuntimeMetadataResolver.getDirectiveMetadata(http:// localhost:8080 / node_modules / angular2 / bundles / angular2.dev .js:22724:47)位于http:// localhost:8080 / node_modules / angular2 / bundles / angular2.dev.js:22792:22位于Array.map(native)的Array.map(http:// localhost:8080) /node_modules/es6-shim/es6-shim.js:1101:14)在TemplateCompiler._compileNestedComponentRuntime的RuntimeMetadataResolver.getViewDirectivesMetadata(http:// localhost:8080 / node_modules / angular2 / bundles / angular2.dev.js:22791:25) (http:// localhost:8080 / node_modules / angular2 / bundles / angular2.dev.js:24763:63)位于http:// localhost:8080 / node_modules / angular2 / bundles / angular2.dev.js:24748:26 at Array.forEach(native)----- async gap -----在Zone.fork的_getStacktraceWithUncaughtError(http:// localhost:8080 / node_modules / angular2 / bundles / angular2-polyfills.js:2244:29)处出错(HTTP://本地主机:8080 / node_modules / angular2 /捆绑/ angular2-polyfi lls.js:2293:47)在bindArguments(http:// localhost:8080 / node_modules /)的Zone.bind(http:// localhost:8080 / node_modules / angular2 / bundles / angular2-polyfills.js:1218:53) angular2 / bundles / angular2-polyfills.js:1401:36)在lib $ es6 $ promise $ promise $$ Promise.obj . (匿名函数)[as then](http:// localhost:8080 / node_modules / angular2 / bundles /angular2-polyfills.js:1413:46)在TemplateCompiler.compileHostComponentRuntime(http:// localhost)的TemplateCompiler.compileComponentRuntime(http:// localhost:8080 / node_modules / angular2 / bundles / angular2.dev.js:24741:14) :8080 / node_modules / angular2 / bundles / angular2.dev.js:24693:14)在DynamicComponentLoader_的RuntimeCompiler.compileInHost(http:// localhost:8080 / node_modules / angular2 / bundles / angular2.dev.js:25044:37) .loadAsRoot(http:// localhost:8080 / node_modules / angular2 / bundles / angular2.dev.js:12253:29)位于di_1.provide.useFactory(http:// localhost:8080 / node_modules / angular2 / bundles / angular2 . dev.js:12351:39)----- async gap ----- _getStacktraceW出错ithUncaughtError(http:// localhost:8080 / node_modules / angular2 / bundles / angular2-polyfills.js:2244:29)在Zone.fork(http:// localhost:8080 / node_modules / angular2 / bundles / angular2-polyfills.js: 2293:47)在新的NgZone(http:// localhost:8080 / node_modules / angular2 / bundles)的NgZone.createInnerZone(http:// localhost:8080 / node_modules / angular2 / bundles / angular2.dev.js:13444:39) /angular2.dev.js:13310:32)在PlatformRef.application的createNgZone(http:// localhost:8080 / node_modules / angular2 / bundles / angular2.dev.js:12373:12)处(http:// localhost:8080) /node_modules/angular2/bundles/angular2.dev.js:12448:31)在执行时在Object.bootstrap(http:// localhost:8080 / node_modules / angular2 / bundles / angular2.dev.js:25239:64)处:// localhost:8080 / app.js:76:23)在Object.execute(http:// http:// localhost:8080 / node_modules / systemjs / dist / system.src.js:2981:26)处于http:/ /localhost:8080/node_modules/systemjs/dist/system.src.js:3099:13)
index.html的:
<!doctype html>
<html>
<head>
<title>Angular 2 - Simple Reddit</title>
<link rel="icon" type="image/png" href="resources/images/favicon-32x32.png" sizes="32x32" />
<link rel="icon" href="resources/images/favicon.ico" />
<!-- Libraries -->
<script src="node_modules/es6-shim/es6-shim.js"></script>
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<!-- Stylesheet -->
<link rel="stylesheet" type="text/css" href="resources/vendor/semantic.min.css">
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<!-- Configure System.js, our module loader -->
<script>
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('app.js')
.then(null, console.error.bind(console));
</script>
<!-- Menu Bar -->
<div class="ui menu">
<div class="ui container">
<a href="#" class="header item">
<img class="logo" src="resources/images/ng-book-2-minibook.png">
ng-book 2
</a>
<div class="header item borderless">
<h1 class="ui header">
Angular 2 Simple Reddit
</h1>
</div>
</div>
</div>
<div class="ui main text container">
<reddit>Loading...</reddit> <!-- <--- Our app loads here! -->
</div>
</body>
</html>
app.ts:
import { bootstrap } from 'angular2/platform/browser';
import { Component } from 'angular2/core';
@Component({
selector: 'reddit-article',
host: {
class: 'row'
},
template: `
<div class="four wide column center aligned votes"> <div class="ui statistic">
<div class="value"> {{ article.votes }}
</div>
<div class="label">
Points
</div>
</div>
</div>
<div class="twelve wide column">
<a class="ui large header" href="{{ article.link }}"> {{ article.title }}
</a>
<ul class="ui big horizontal list voters">
<li class="item">
<a href (click)="voteUp()">
<i class="arrow up icon"></i> upvote
</a> </li>
<li class="item">
<a href (click)="voteDown()">
<i class="arrow down icon"></i>
downvote
</a> </li>
</ul> </div>
`
})
class Article {
title: string;
link: string;
votes: number;
constructor(title: string, link: string, votes?: number) {
this.title = title;
this.link = link;
this.votes = votes || 0;
}
voteUp(): void {
this.votes += 1;
}
voteDown(): void {
this.votes -= 1;
}
}
class ArticleComponent {
article: Article;
constructor() {
this.article = new Article('Angular 2', 'http://angular.io', 10);
}
voteUp(): boolean {
this.article.voteUp();
return false;
}
voteDown(): boolean {
this.article.voteDown();
return false;
}
}
@Component({
selector: 'reddit',
directives: [ArticleComponent],
template: `
<form class="ui large form segment">
<h3 class="ui header">Add a Link</h3>
<div class="field">
<label for="title">Title:</label> <input name="title" #newtitle>
</div>
<div class="field">
<label for="link">Link:</label>
<input name="link" #newlink>
</div>
<button (click)="addArticle(newtitle, newlink)"
class="ui positive right floated button">
Submit link
</button>
</form>
<div class="ui grid posts">
<reddit-article>
</reddit-article>
</div>`
})
class RedditApp {
constructor() {
}
addArticle(title: HTMLInputElement, link: HTMLInputElement): void {
console.log(`Adding article title: ${title.value} and link: ${link.value}`);
}
}
bootstrap(RedditApp);
2 回答
您在错误的类上定义
@Component
:Article
而不是ArticleComponent
. 您在RedditApp
类的directives
属性中设置ArticleComponent
类 .你可以这样改变:
你在
directives
的directives
中列出ArticleComponent
但是ArticleComponent
isn 't a directive. It doesn't有一个@Directive(...)
或@Component(...)
装饰器 .