2016-06-07 2 views
0

Я изучаю Angular2 с использованием JavaScript (No TypeScript) с помощью this video. Код работал нормально, пока я не начал использовать service. Он начинает давать мне следующую ошибку -Angular2 Uncaught ReferenceError: Class не определен

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

use 'ng.core.Class' посмотреть здесь: https://angular.io/docs/js/latest/guide/cheatsheet.html – zpul

+0

Отлично! Это сработало. Благодаря :-) – noob

Смежные вопросы