繁体   English   中英

Angular2:无法加载组件

[英]Angular2: Unable to load components

我正在练习angular2,并且尝试使用属性指令,但是无法加载组件,也不知道我做错了什么

朋克: http ://plnkr.co/edit/o3llvK?p=preview

应用程式:

//our root app component
import {Component} from 'angular2/core'
import {AttributeDirective} from './attributedirective.component'

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <my-attr-directive></my-attr-directive>
    </div>
  `,
  directive: [AttributeDirective]
})
export class App {
  constructor() {
    this.name = 'Angular2'
  }
}

attributedirective.component.ts:

import {Component} from 'angular2/core'
import {HighlightDirective} from './highlight.directive'

@Component({
  selector: 'my-attr-directive',
  template: `
    <div myHighlight>Highlight me</div>
  `,
  directive: [HighlightDirective]
})
export class AttributeDirective{
  console.log("test"); 
}

Highlight.directive.ts:

import {Directive, ElementRef, OnInit} from 'angular2/core'

@Directive({
  selector: '[myHighlight]'
})

export class HighlightDirective implements OnInit{
  private _defaultGreen = 'green';

  constructor (private _elRef: ElementRef){}

  ngOnInit():any{
    this._elRef.nativeElement.style.backgroundColor = this._defaultGreen;
  }
}

有人可以帮我解决这个问题吗?

这将修复您的unk ...

export class AttributeDirective{
  consturctor(){
      console.log("test");   // you can't use console.log directly as you are working with typescript not javascript....
  }
}

暂无
暂无

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

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