简体   繁体   English

Angular2未捕获ReferenceError:未定义类

[英]Angular2 Uncaught ReferenceError: Class is not defined

I am learning Angular2 using JavaScript (No TypeScript) with the help of this video . 此视频的帮助下,我正在使用JavaScript (无TypeScript)学习Angular2 The code worked fine, until I started using a service . 在我开始使用service之前,代码运行良好。 It starts to give me the following error - 它开始给我以下错误-

Uncaught ReferenceError: Class is not defined

I understand that Class is not defined in JavaScript, but I assume that it's a part of Angular2 library. 我知道Class不是用JavaScript定义的,但我认为它是Angular2库的一部分。 Here's my complete app.js code - 这是我完整的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"
    }
  ];
})();

This line var QuoteService = Class({...}); 这行var QuoteService = Class({...}); is responsible for this error. 负责此错误。 Any idea what could be going wrong in this case? 知道在这种情况下可能出什么问题吗?

I think you should write as follows: 我认为您应该这样写:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM