angular - ts 中的父级和祖先注释

标签 angular typescript

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

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

使用任一注释都会抛出“TypeError:装饰器不是函数”。

我使用的是 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/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a1c3ced6c4d38cd5d3c0c2c4d4d38cd3d4cfd5c8ccc4e1918f918f9998" rel="noreferrer noopener nofollow">[email protected]</a>/traceur-runtime.js"></script>
    <script src="https://jspm.io/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7d4ded4d3c2cae797899691899089cdd4" rel="noreferrer noopener nofollow">[email protected]</a>"></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>

最佳答案

对我来说,这是通过使用原始形式但在模块“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);

关于angular - ts 中的父级和祖先注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30149559/

相关文章:

angular - 如何让组件在angular2中检测到它的隐藏状态?

javascript - 无法读取未定义的给定属性

angular - 将 Observable 映射到对象数组

node.js - 更新到 Angular 8 CLI 后抛出 ".getColorDepth is not a function"

javascript - Angular 2 View 在 ngOnInit 中加载数据之前呈现

angular - typescript 类继承

angular - 动态更改 Angular 6 中的图标

javascript - 如何使用 Angular 将动态搜索框放在表格上?

javascript - 将嵌入的内容传递给嵌套列表的孙组件

javascript - 使用 private 时,Typescript 类无法访问