Angular 2,主要组件内的组件

标签 angular

我在 angular.io 上玩过 angular 2 演示。 现在我想创建一个新组件并在我的应用程序中使用它。

我当前的应用:

import {Component, Template, bootstrap} from 'angular2/angular2';
import {UsersComponent} from './components/users/component.js';

// Annotation section
@Component({
  selector: 'my-app'
})
@Template({
  url:"app.html"
})
// Component controller
class MyAppComponent {
  newName:string;
  names:Array<string>;
  constructor() {
    this.name = 'Florian';
  }

  showAlert(){
    alert("It works");
  }

  addName(){
    names.push(newName);
    newName = "";
  }
}

bootstrap(MyAppComponent);

我的组件:

import {Component, Template, bootstrap} from 'angular2/angular2';
// Annotation section
@Component({
  selector: 'users'
})
@Template({
  url:"./template.html"
})
// Component controller
class UsersComponent {
  newName:string;
  names:Array<string>;
  constructor() {

  }

  addName(){
    names.push(newName);
    newName = "";
  }
}

我不确定我的用户组件的语法是否正确。

我的应用模板:

<h1>Hello {{ name }}</h1>
<h2>{{2+2}}</h2>
<hr />
<button (click)="showAlert()">Click to alert</button>
<users></users>

那么我该如何连接用户组件呢?

我在控制台中得到的错误:

 "Error loading "components/users/component.js" at <unknown>↵Error loading "components/users/component.js" from "app" at http://localhost:8080/app.es6↵Cannot read property 'replace' of undefined"

最佳答案

Angular 日历演示有两个嵌套组件(calendar 和 calendar-cell)https://github.com/djsmith42/angular2_calendar.git .据我所见,您的 MyAppComponent 应该从 @Template 的指令列表中引用 Users 组件,如下所示:

@Template({
  url: System.baseURL+'app/components/calendar/calendar.html',
  directives: [
    Foreach,
    If,
    CalendarCell
  ]
})

关于Angular 2,主要组件内的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29121678/

相关文章:

angular - 如何从可观察的Angular2分配局部变量

node.js - angular2 quick start 只显示 Loading .. 而不是 index.html 的内容

node.js - Ng 命令未找到 Angular

node.js - ng-socket-io允许房间/加入房间吗?如果没有,如何让特定用户加入 session 室?

angular - File 'app/hero.ts' is not a module error in the console, where to store interfaces files in directory structure with angular2?

Angular 4 : setting the local variable # dynamically

angular - 空注入(inject)器错误: No provider for MatBottomSheet

json - 在 Angular 中从 json 获取键和值

angular - typescript : Colon vs Equal To ( Angular Tutorial )

javascript - 无法正确重新绑定(bind) imagemapster 以设置新的工具提示(TypeError : Cannot read property '0' of null)