【问题标题】:Angular 2 tutorial quickstart doesn't load the componentAngular 2 教程快速入门不加载组件
【发布时间】:2017-04-26 21:22:22
【问题描述】:

我开始学习 Angular,因此想做他们的教程。 但是,它只是不加载组件。 我正是这样做的:

  1. git clone https://github.com/angular/quickstart.git 快速入门
  2. cd 快速入门
  3. npm 安装
  4. npm 开始

我还没有修改代码,安装运行良好,没有任何错误。 但是,当启动应用程序时,浏览器只显示“正在加载 AppComponent 内容...”而不是预期的“Hello Angular”。 浏览器控制台给了我以下错误:

Error: (SystemJS) in strict mode code, functions may be declared only at top level or immediately within another function
    ZoneDelegate.prototype.invoke@http://localhost:3000/node_modules/zone.js/dist/zone.js:229:17
    Zone.prototype.run@http://localhost:3000/node_modules/zone.js/dist/zone.js:113:24
    scheduleResolveOrReject/<@http://localhost:3000/node_modules/zone.js/dist/zone.js:509:52
    ZoneDelegate.prototype.invokeTask@http://localhost:3000/node_modules/zone.js/dist/zone.js:262:21
    Zone.prototype.runTask@http://localhost:3000/node_modules/zone.js/dist/zone.js:151:28
    drainMicroTaskQueue@http://localhost:3000/node_modules/zone.js/dist/zone.js:405:25
    ZoneTask/this.invoke@http://localhost:3000/node_modules/zone.js/dist/zone.js:336:25

    Evaluating http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js
    Error loading http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js as "@angular/compiler" from http://localhost:3000/node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js


var error = NativeError.apply(this, arguments);

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `<h1>Hello {{name}}</h1>`,
})
export class AppComponent  { name = 'Angular'; }

app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }  from './app.component';

@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

package.json

{
  "name": "angular-quickstart",
  "version": "1.0.0",
  "description": "QuickStart package.json from the documentation, supplemented with testing support",
  "scripts": {
    "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
    "e2e": "tsc && concurrently \"http-server -s\" \"protractor protractor.config.js\" --kill-others --success first",
    "lint": "tslint ./app/**/*.ts -t verbose",
    "lite": "lite-server",
    "pree2e": "webdriver-manager update",
    "test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
    "test-once": "tsc && karma start karma.conf.js --single-run",
    "tsc": "tsc",
    "tsc:w": "tsc -w"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "dependencies": {
    "@angular/common": "~2.3.0",
    "@angular/compiler": "~2.3.0",
    "@angular/core": "~2.3.0",
    "@angular/forms": "~2.3.0",
    "@angular/http": "~2.3.0",
    "@angular/platform-browser": "~2.3.0",
    "@angular/platform-browser-dynamic": "~2.3.0",
    "@angular/router": "~3.3.0",

    "angular-in-memory-web-api": "~0.1.17",
    "systemjs": "0.19.40",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-rc.4",
    "zone.js": "^0.7.2"
  },
  "devDependencies": {
    "concurrently": "^3.1.0",
    "lite-server": "^2.2.2",
    "typescript": "~2.0.10",

    "canonical-path": "0.0.2",
    "http-server": "^0.9.0",
    "tslint": "^3.15.1",
    "lodash": "^4.16.4",
    "jasmine-core": "~2.4.1",
    "karma": "^1.3.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "4.0.9",
    "webdriver-manager": "10.2.5",
    "rimraf": "^2.5.4",

    "@types/node": "^6.0.46",
    "@types/jasmine": "^2.5.36",
    "@types/selenium-webdriver": "^2.53.33"
  },
  "repository": {}
}

【问题讨论】:

  • 你能显示你的 package.json 文件吗?
  • @H.Herzl 我已将 package.json 添加到我的问题中。
  • 检查 index.html 文件并找到行:System.import()。检查导入中提到的文件。在该文件中,检查被引导的组件。

标签: angular


【解决方案1】:

我有同样的问题,我发现了这个: angular2 quickstart ReferenceError: angular is not defined new angular.Component({

看来是火狐浏览器有问题,Chrome没有问题……

【讨论】:

  • 谢谢!我实际上认为错误是违反了 zone.js 中的严格模式。但是,Chrome 似乎并不关心它,它在那里运行良好。
【解决方案2】:

我遇到了同样的问题,我通过以下方式解决了它: 安装 angular2@2.0.0-beta.21 并卸载出现错误的组件,如 zone.js、core.js,清理缓存并重新安装它们和 systemjs。

当您卸载 zone.js 时,您从浏览器控制台和 cmd 日志中得到的错误可以或多或少地指导您进行后续步骤。

【讨论】:

    猜你喜欢
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 2023-03-31
    相关资源
    最近更新 更多