2016-06-07 137 views
0

我正在使用JavaScript(No TypeScript)在this video的帮助下学习Angular2。代码工作正常,直到我开始使用service。它开始给我下面的错误 -Angular2未捕获ReferenceError:未定义类

Uncaught ReferenceError: Class is not defined 

我明白,类不是在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({...});负责这个错误。任何想法在这种情况下会出现什么问题?

回答

2

我想你应该写如下:

var Class = ng.core.Class; 
var QuoteService = Class({ ... 

https://angular.io/api/core/Class

+0

感谢您的链接,但它仍然无法正常工作。现在我得到'ng.class不是函数'。 – noob

+1

使用'ng.core.Class'看看这里:https://angular.io/docs/js/latest/guide/cheatsheet.html – zpul

+0

太棒了!有效。谢谢 :-) – noob

相关问题