我正在使用Angular CLI v1.0.0-beta.19-3来构建一个Angular 2网站 . 我生成我的组件如下 ng g component file-upload
.
我注意到生成的组件 file-upload.component.ts
如下所示 .
@Component({
selector: 'file-upload',
templateUrl: './file-upload.component.html',
styleUrls: ['./file-upload.component.css']
})
export class FileUploadComponent implements OnInit {
...
}
我需要在我自己的组件中使用这个组件ng2-file-upload . 所以我尝试在 @Component
装饰器上添加 directives
属性,但是,我在VS Code中收到错误 .
@Component({
selector: 'file-upload',
templateUrl: './file-upload.component.html',
styleUrls: ['./file-upload.component.css'],
directives: [ FILE_UPLOAD_DIRECTIVES ]
})
export class FileUploadComponent implements OnInit {
...
}
[ts] Argument of type '{ selector: string; templateUrl: string; styleUrls: string[]; directives: undefined[]; }' is not assignable to parameter of type 'Component'.
Object literal may only specify known properties, and 'directives' does not exist in type 'Component'.
我的导入如下所示 .
import { Component, OnInit, Input } from '@angular/core';
import { NgClass, NgStyle } from '@angular/common';
import { FileSelectDirective, FileDropDirective, FileUploadModule, FileUploader, FileUploaderOptions } from 'ng2-file-upload';
当我运行应用程序 ng serve
时,我看到控制台中报告了以下错误 .
zone.js:388 Unhandled Promise rejection: Template parse errors:
Can't bind to 'uploader' since it isn't a known property of 'div'. ("
[ngClass]="{'nv-file-over': hasBaseDropZoneOver}"
(fileOver)="fileOverBase($event)"
[ERROR ->][uploader]="uploader"
class="well my-drop-zone">
这是 package.json
中的依赖项 .
{
"name": "app-www",
"version": "0.0.0",
"license": "MIT",
"angular-cli": {},
"scripts": {
"start": "ng serve",
"lint": "tslint \"src/**/*.ts\"",
"test": "ng test",
"pree2e": "webdriver-manager update",
"e2e": "protractor"
},
"private": true,
"dependencies": {
"@angular/common": "~2.1.1",
"@angular/compiler": "~2.1.1",
"@angular/core": "~2.1.1",
"@angular/forms": "~2.1.1",
"@angular/http": "~2.1.1",
"@angular/platform-browser": "~2.1.1",
"@angular/platform-browser-dynamic": "~2.1.1",
"@angular/router": "~3.1.1",
"angular2-cookie": "^1.2.5",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"jquery": "^3.1.1",
"lodash": "^4.16.6",
"ng2-bootstrap": "^1.1.16",
"ng2-file-upload": "^1.1.2",
"rxjs": "5.0.0-beta.12",
"ts-helpers": "^1.1.1",
"zone.js": "^0.6.25"
},
"devDependencies": {
"@types/jasmine": "^2.2.30",
"@types/lodash": "^4.14.38",
"@types/node": "^6.0.45",
"angular-cli": "1.0.0-beta.19-3",
"codelyzer": "1.0.0-beta.1",
"jasmine-core": "2.4.1",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "4.0.9",
"ts-node": "1.2.1",
"tslint": "3.13.0",
"typescript": "~2.0.3",
"webdriver-manager": "10.2.5"
}
}
该文档说使用此import语句 .
import { FILE_UPLOAD_DIRECTIVES } from 'ng2-file-upload';
但在VS Code中,它说如下 .
[ts] Module '"/Users/jwayne/git/app-www/node_modules/ng2-file-upload/ng2-file-upload"' has no exported member 'FILE_UPLOAD_DIRECTIVES'.
更有趣的是,我创建了自己的登录组件,如下所示 .
@Component({
selector: 'login-component',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
...
}
为了在另一个组件中使用它,我甚至不必将它作为指令或类似的东西引用它 . 例如,我有一个使用此登录组件的默认组件 . HTML如下所示 .
<login-component></login-component>
代码如下所示(注意组件装饰器没有指令属性) .
@Component({
selector: 'app-default',
templateUrl: './default.component.html',
styleUrls: ['./default.component.css']
})
export class DefaultComponent implements OnInit, OnDestroy {
...
}
虽然现在修改了 app.module.ts
文件 .
import { LoginComponent } from './login/login.component';
import { DefaultComponent } from './default/default.component';
@NgModule({
declarations: [
AppComponent,
LoginComponent,
DefaultComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
RouterModule.forRoot([
{ path: 'default', component: DefaultComponent },
{ path: 'main', component: MainComponent, canActivate: [ UserAuthGuard ] },
{ path: '', redirectTo: '/default', pathMatch: 'full' },
{ path: '**', redirectTo: '/default' }
])
],
providers: [
UserAuthGuard
],
bootstrap: [AppComponent]
})
export class AppModule { }
所以,有很多事情要发生,但首先,组件装饰器的指令属性发生了什么?
2 回答
似乎文档使用的是Angular的旧版RC版本 . 与此同时,角度团队引入了
NgModule
的概念,以更好地构建您的应用程序并避免重复使用的指令 . 因此,您不再声明Component
装饰器中使用的指令 . 有关详细信息,请查看angular docs on NgModule .您正在使用的库也适用于此,但可能文档尚未更改 . 检查存储库中的their demo code和this commit以查看您现在需要如何导入模块,还可以查看API更改 .
解决此问题的关键是在
app.module.ts
中导入指令,然后使用@NgModule
声明它们 . 这是app.module.ts
的片段 .在我的组件中,我仍然需要导入指令 . 这是另一个片段 .