我在this video的帮助下使用Angular2
(无TypeScript)学习JavaScript
。代码工作正常,直到我开始使用service
。它开始给我以下错误 -
Uncaught ReferenceError: Class is not defined
我知道Class没有在JavaScript中定义,但我认为它是Angular2库的一部分。这是我完整的app.js代码 -
(function() {
var Component = ng.core.Component;
var bootstrap = ng.platformBrowserDynamic.bootstrap;
var QuoteService = Class({
constructor:function () {
this.quotes = quotes;
},
getRandomQuote: function (params) {
var randomIndex = Math.floor(Math.random()*quotes.length);
return this.quotes[randomIndex];
}
});
var RandomQuoteComponent = Component({
selector:'random-quote',
template:'<em>{{quote.line}}</em> - {{quote.author}}'
})
.Class({
constructor:function () {
var quoteService = new QuoteService();
this.quote = quoteService.getRandomQuote();
}
});
var AppComponent = Component({
selector: 'my-app',
directives:[RandomQuoteComponent],
template:
'<h1>Random Quotes</h1>'+
'<p><random-quote></random-quote></p>'
})
.Class({
constructor: function () {
//empty
}
});
document.addEventListener('DOMContentLoaded',function () {
bootstrap(AppComponent);
})
var quotes = [
{
"line": "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.",
"author": "Brian W. Kernighan"
},
{
"line": "Walking on water and developing software from a specification are easy if both are frozen.",
"author": "Edward V Berard"
},
{
"line": "It always takes longer than you expect, even when you take into account Hofstadter's Law.",
"author": "Hofstadter's Law"
},
{
"line": "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.",
"author": "Rick Osborne"
},
{
"line": "In theory, there is no difference between theory and practice. But, in practice, there is.",
"author": "Jan L. A. van de Snepscheut"
},
{
"line": "Measuring programming progress by lines of code is like measuring aircraft building progress by weight.",
"author": "Bill Gates"
},
{
"line": "There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors.",
"author": "Leon Bambrick"
},
{
"line": "Nine people can't make a baby in a month.",
"author": "Fred Brooks"
},
{
"line": "If debugging is the process of removing software bugs, then programming must be the process of putting them in.",
"author": "Edsger Dijkstra"
},
{
"line": "The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.",
"author": "Tom Cargill"
}
];
})();
此行var QuoteService = Class({...});
负责此错误。知道在这种情况下会出现什么问题吗?
答案 0 :(得分:2)
我认为你应该写如下:
var Class = ng.core.Class;
var QuoteService = Class({ ...