本文介绍了Angular2 版本 RC.6 “指令"@Component 内部错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Angular2,并且已经从官方网站下载了 package.json.当我尝试在 @Component 装饰器中使用指令"时,出现此错误.

I am using Angular2 and have downloaded package.json from the official website. When I am trying to use "directives" inside @Component decorator I am getting this error.

我附上了我的代码错误:

I have attached my code ERROR:

[ts]
Argument of type '{ selector: string; template: string; directives: string;
}' is not assignable to parameter of type 'ComponentMetadataType'.

Object literal may only specific known properties, and 'directives' does not
exist in type 'ComponentMetadataType'.
(property) directives: string

这是我的代码:

import { Component } from '@angular/core';
import { PeopleListComponent } from './people-list/people-list.component';

@Component({
 selector: 'my-app',
 template: '<h1>{{ title }}</h1>',
 directives: ''   //ERROR //NOT ABLE TO RECOGNIZE
})

export class AppComponent {
  title = "My title";
}

这是我的 package.json:

Here is my package.json:

        {
          "name": "angular2-quickstart",
          "version": "1.0.0",
          "scripts": {
            "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
            "lite": "lite-server",
            "postinstall": "typings install",
            "tsc": "tsc",
            "tsc:w": "tsc -w",
            "typings": "typings"
          },
          "license": "ISC",
          "dependencies": {
            "@angular/common": "2.0.0-rc.6",
            "@angular/compiler": "2.0.0-rc.6",
            "@angular/compiler-cli": "0.6.0",
            "@angular/core": "2.0.0-rc.6",
            "@angular/forms": "2.0.0-rc.6",
            "@angular/http": "2.0.0-rc.6",
            "@angular/platform-browser": "2.0.0-rc.6",
            "@angular/platform-browser-dynamic": "2.0.0-rc.6",
            "@angular/router": "3.0.0-rc.2",
            "@angular/upgrade": "2.0.0-rc.6",
            "core-js": "^2.4.1",
            "reflect-metadata": "^0.1.3",
            "rxjs": "5.0.0-beta.11",
            "systemjs": "0.19.27",
            "zone.js": "^0.6.17",
            "angular2-in-memory-web-api": "0.0.18",
            "bootstrap": "^3.3.6"
          },
          "devDependencies": {
            "concurrently": "^2.2.0",
            "lite-server": "^2.2.2",
            "typescript": "^1.8.10",
            "typings":"^1.3.2"
          }
        }

推荐答案

简单...

问题在于指令:''

problem is with directives: ''

作为一个数组,它应该是 directives: [ ]

as its an array it should be directives: [ ]

更新:在 RC6 OR 更高版本中,您有 @NgModule.在RC6 OR以后的版本中,你必须声明pipes指令 您将在 @NgModule 中使用,如图所示...

Update: In RC6 OR later version, you have @NgModule. In RC6 OR later version, you have to declare pipes and directives that you are gonna use within @NgModule as shown...

import { PeopleListComponent } from './people-list/people-list.component';

@NgModule({
  imports:      [ BrowserModule],
  declarations: [ AppComponent,PeopleListComponent ],  //<<===here
  providers:    [],
  bootstrap:    [ AppComponent ]
})

您可以从 AppComponent@Component 中删除 directives:''/code>

这篇关于Angular2 版本 RC.6 “指令"@Component 内部错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 02:35