我正在编写一个依赖于TemplateRef<any>
and的指令ViewContainerRef
。但是我的指令无法注入这些依赖项。以下是我的所有代码:
main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HelloWorld } from './HelloWorld.directive';
@NgModule({
imports: [BrowserModule],
declarations: [AppComponent, HelloWorld],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
Hello world!
<template [lcngHw]="true"><div></div></template>
`
})
export class AppComponent {
}
HelloWorld.directive.ts
import { Directive } from '@angular/core';
import { Input } from '@angular/core';
import { TemplateRef, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[lcngHw]'
})
export class HelloWorld{
constructor(private tf: TemplateRef<any>, private vc: ViewContainerRef){
}
@Input()
set lcngHw(value: boolean) {
if (value) {
this.vc.createEmbeddedView(this.tf);
}
else {
this.vc.clear();
}
}
}
这是我转译的 HelloWorld.directive.js:
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var core_1 = require('@angular/core');
var core_2 = require('@angular/core');
var HelloWorld = (function () {
function HelloWorld(tf, vc) {
this.tf = tf;
this.vc = vc;
}
Object.defineProperty(HelloWorld.prototype, "lcngHw", {
set: function (value) {
if (value) {
this.vc.createEmbeddedView(this.tf);
}
else {
this.vc.clear();
}
},
enumerable: true,
configurable: true
});
__decorate([
core_2.Input()
], HelloWorld.prototype, "lcngHw", null);
HelloWorld = __decorate([
core_1.Directive({
selector: '[lcngHw]'
})
], HelloWorld);
return HelloWorld;
}());
exports.HelloWorld = HelloWorld;
然后我运行我的应用程序,我收到以下错误:
错误:(SystemJS)无法解析 HelloWorld 的所有参数:(?,?)。
错误:无法解析 HelloWorld 的所有参数:(?, ?)。在 CompileMetadataResolver.getDependenciesMetadata ( http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:14268:21 ) 在 CompileMetadataResolver.getTypeMetadata ( http://localhost:5000/node_modules/@angular/ compiler/bundles/compiler.umd.js:14169:28 ) 在 CompileMetadataResolver.getDirectiveMetadata ( http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:13944:30 ) 在 eval ( http ://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:14037:51 ) 在 Array.forEach (native) 在 CompileMetadataResolver.getNgModuleMetadata (http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:14031:51 ) 在 RuntimeCompiler._compileComponents ( http://localhost:5000/node_modules/@angular/compiler/bundles/compiler .umd.js:16721:49 ) 在 RuntimeCompiler._compileModuleAndComponents ( http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:16659:39 ) 在 RuntimeCompiler.compileModuleAsync ( http:// localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:16650:23 ) 在 PlatformRef_._bootstrapModuleWithZone ( http://localhost:5000/node_modules/@angular/core/bundles/core.umd.js :6707:29 ) 评估http://localhost:5000/ViewContainerRefApp/main.js 在 CompileMetadataResolver.getDependenciesMetadata ( http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:14268:21 ) 处 加载http://localhost:5000/ViewContainerRefApp/main.js时出错。 GetTypeMetadata ( http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:14169:28 ) 在 CompileMetadataResolver.getDirectiveMetadata ( http://localhost:5000/node_modules/@angular/compiler/bundles /compiler.umd.js:13944:30 ) 在 eval ( http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:14037:51 ) 在 Array.forEach (native) 在 CompileMetadataResolver .getNgModuleMetadata (http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:14031:51 ) 在 RuntimeCompiler._compileComponents ( http://localhost:5000/node_modules/@angular/compiler/bundles/compiler .umd.js:16721:49 ) 在 RuntimeCompiler._compileModuleAndComponents ( http://localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:16659:39 ) 在 RuntimeCompiler.compileModuleAsync ( http:// localhost:5000/node_modules/@angular/compiler/bundles/compiler.umd.js:16650:23 ) 在 PlatformRef_._bootstrapModuleWithZone ( http://localhost:5000/node_modules/@angular/core/bundles/core.umd.js :6707:29 ) 评估http://localhost:5000/ViewContainerRefApp/main.js 加载http://localhost:5000/ViewContainerRefApp/main.js 时出错
我还从这个实时示例中复制了代码,我得到了同样的错误。
似乎TemplateRef<any>
andViewContainerRef
没有metadata
注册。但我找不到原因。任何人都可以帮助我吗?谢谢你。