1

尝试将ParentAncestor注释添加到 TypeScript 类型,所以我这样做了:

declare module "angular2/src/core/annotations_impl/visibility"{
   function Parent():(target: any) => any;
   function Ancestor():(target: any) => any;
}

使用任何一个注释都会抛出"TypeError: decorator is not a function".

我正在使用 alpha 22。

为什么我会收到此错误?

这是一个示例:

/// <reference path="typings/angular2/angular2.d.ts" />
import {Component,View,bootstrap} from "angular2/angular2"
import {Ancestor,Parent} from "angular2/src/core/annotations_impl/visibility"

@Component({
    selector:"c"
})
@View({
    template:`<p>{{app.message}}</p>`
})
class C{
    app:App;
    constructor(@Ancestor() app:App){
        this.app = app;
    }
}

@Component({
    selector:"app"
})
@View({
    template:`<c></c>`,
    directives:[C]
})
class App{
    message = "test";
}
bootstrap(App);

HTML:

<html>
  <head>
    <title>Angular 2 Quickstart</title>
    <script src="https://github.jspm.io/jmcriffey/bower-traceur-runtime@0.0.89/traceur-runtime.js"></script>
    <script src="https://jspm.io/system@0.16.7.js"></script>
    <script src="https://code.angularjs.org/2.0.0-alpha.22/angular2.dev.js"></script>
  <body>
    <!-- The app component created in app.ts -->
    <app></app>
    <script>
      System.import('app');
    </script>
  </body>
</html>
4

1 回答 1

2

对我来说,这可以通过使用您的原始表单但在模块“angular2/annotations”中声明它来实现。

抱歉,我没有尝试您的示例,而是 Pascal Precht 的选项卡示例。我不确定您是否可以将 App 视为 C 的祖先,因为“App 已被 C 替换”。

以下改编对我有用。当 B 和 C 的定义顺序相反时,我仍然在下面得到你的错误,这看起来很愚蠢。

import {bootstrap, Component, View, For} from 'angular2/angular2';
import {Ancestor,Parent} from 'angular2/annotations';


@Component({
   selector:"b"
})
@View({
   template:"<content></content>"
})
class B{
   message : string = "test";
}

@Component({
   selector:"c"
})
@View({
   template:`<p>{{b.message}}</p>`
})
class C{
   b:B = null;
   constructor(@Ancestor() b:B){
      this.b = b;
   }
}



@Component({
  selector:"app"
})
@View({
   template:`
   <b>
      <c></c>
   </b>
`,
directives:[B,C]
})
class App{

}


bootstrap(App);
于 2015-05-11T00:45:46.397 回答