【问题标题】:Angular 2 Heroes Tutorial error: The selector "my-app" did not match any elementsAngular 2 Heroes 教程错误:选择器“my-app”不匹配任何元素
【发布时间】:2017-08-29 13:58:31
【问题描述】:

我正在关注angular官网的英雄教程。

我使用 CLI 生成了项目。

在第 6 部分路由之前一切正常:https://angular.io/tutorial/toh-pt5

在路由之前更新了代码,教程说:

应用仍在运行并显示英雄。

但是,我收到以下错误:

选择器“my-app”没有匹配任何元素

来自 Chrome 开发控制台。

我尝试将index.html 中的app-root 更改为my-app,或者将app.component.tsselector 值中的my-app 更改为app-root。没有任何帮助。

这是我在过去一年中第二次从头开始尝试本教程,但我相信我面临同样的问题(不太记得我上次遇到的确切问题,但由于一些错误)。这让我觉得教程有问题。

但是,当我用谷歌搜索时,我找不到其他遇到此问题的人。有几个人有

我的应用不匹配任何元素

问题,但不是来自英雄教程。

还有其他人尝试过英雄教程吗?你有过这个问题吗?我被困在这一点上,无法继续本教程。

你想要我的代码吗?正如我所说的,它与在将路由添加到方程式之前在教程中给出的相同。

谢谢。

更新:

我说我的代码和教程里的一样,哦,好吧,有一些区别:1)我使用templateUrl,教程使用模板,2)我没有使用那些大字体h1,我使用的是分区。

因此,相关代码在下面,但除非您按照教程 CLI 对项目进行了 CLI,否则这不会让您继续前进,因为项目中有大量其他文件。我很好奇,有没有人成功地完成了整个教程而没有遇到任何问题?如果是,那么我可能做错了,否则,我怀疑教程有问题。

//app.component.ts:
import { Component } from '@angular/core';

@Component({
  selector: 'my-app',  
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']  
})

export class AppComponent {
  title = 'Tour of Heroes';  
}

//app.component.html
<!--The content below is only a placeholder and can be replaced.-->
<div>
    Welcome to {{title}}!
    <my-heroes></my-heroes>
</div>

//app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { HeroDetailComponent } from './hero-detail.component';
import { HeroesComponent } from './heroes.component';
import { HeroService } from './hero.service';

@NgModule({
  imports: [
    BrowserModule,
    FormsModule
  ],
  declarations: [
    AppComponent,
    HeroDetailComponent,
    HeroesComponent
  ],
  providers: [HeroService],
  bootstrap: [AppComponent]
})
export class AppModule { }

//hero-detail.component.ts
import { Component, Input } from '@angular/core';
import { Hero } from './hero';

@Component({
   selector: 'hero-detail',
   templateUrl: 'hero-detail.component.html'
})
export class HeroDetailComponent {
    @Input() hero: Hero;
}

//hero-detail.component.html
<!--The content below is only a placeholder and can be replaced.-->
<div>
    <div *ngIf="hero">
        <div>{{hero.name}} details</div>
        <div><label>id:</label>{{hero.id}}</div>
        <div><label>name:</label><input [(ngModel)]="hero.name" placeholder="name" /></div>
    </div>    
</div>

//hero.service.ts
import { Injectable } from '@angular/core';
import {Hero} from './hero';
import {HEROES} from './mock-heroes';

@Injectable()
export class HeroService { 
  getHeroes(): Promise<Hero[]> {
     return Promise.resolve(HEROES);
  }

  getHeroesSlowly(): Promise<Hero[]> {
    return new Promise(resolve=> {
      setTimeout(()=>resolve(this.getHeroes()), 2000);
    });
  }
}

//hero.ts
export class Hero {
 id: number;
 name: string;
 }

//heroes.component.html
import { Component, OnInit } from '@angular/core';
import { Hero } from './hero';
import { HeroService } from './hero.service';

@Component({
  selector: 'my-heroes',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']  
})

export class HeroesComponent implements OnInit {
  title = 'Tour of Heroes';
  heroes: Hero[];  
  selectedHero: Hero;

  constructor(private heroService: HeroService) {}

  onSelect(hero: Hero): void{
    this.selectedHero = hero;
  }

  getHeroes(): void {
    this.heroService.getHeroesSlowly().then(heroes => this.heroes = heroes);
  }

  ngOnInit(): void {
    this.getHeroes();
  }
}

//mock-heroes.ts
import {Hero} from './hero';

export const HEROES: Hero[] = [
    {id: 11, name: 'Mr. Nice'},
    {id: 12, name: 'Narco'},
    {id: 13, name: 'Bombasto'},
    {id: 14, name: 'Celeritas'},
    {id: 15, name: 'Magneta'},
    {id: 16, name: 'RubberMan'},
    {id: 17, name: 'Dynama'},
    {id: 18, name: 'Dr IQ'},
    {id: 19, name: 'Mr. Nice'},
    {id: 20, name: 'Magma'},
    {id: 21, name: 'Tornado'}   
  ];

//index.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My NG App</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <my-root></my-root>
</body>
</html>

//main.ts
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule);

【问题讨论】:

  • 你的AppComponent 类是什么样子的,你是在声明@Component({ selector: 'my-app'... 之类的选择器还是什么?
  • 是的,如果您可以发布您的 index.html 文件内容和您的 AppComponent,那将会很有帮助。这样我们就不必通过教程来达到你目前的水平。 :-)
  • @LenilsondeCastro:我看过你链接的那个。看起来相似,但不一样。
  • @DeborahK:我已经用相关代码更新了帖子。您将需要 CLI 英雄项目并插入我的相关文件以进行测试。谢谢。

标签: angular


【解决方案1】:

看起来像这样:

<body>
  <my-root></my-root>
</body>

必须是这样的:

<body>
  <my-app></my-app>
</body>

在 index.html 文件中。

为了回答您的问题,在 4 月的 ngConf 上,他们为开发人员提供了一个学习教程的房间,而我在房间里工作的那一天几乎占满了。看起来他们基本上都是成功的(让 npm install 在每个人的系统上工作的大部分困难。)但我一直没有密切关注,以了解从那时起文档的那个区域是否发生了变化。 (我在文档团队,但主要从事指南工作,而不是教程)。

【讨论】:

  • 是的 ^ OP - 如果您已经知道这一点,请原谅我 - 但选择器是编译器将查找以呈现您的组件的 html 元素。对于 root / app 组件,它会在你的 index.html 文件中查找该元素,并在找到它时引导(“启动”)应用程序。如果有不匹配 - 没有应用程序!你可以给它起任何名字,只要它们匹配。因此,如果您的应用程序组件中的选择器是“bla-bla-bla”,您只需要确保在您的 index.html 文件中有 祝你好运!
猜你喜欢
  • 2018-10-28
  • 2016-06-09
  • 2016-11-24
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多