Generic type &#39;Array<t>&#39; requires 1 type argument(s). - Angular2

时间:2017-07-01 19:25:04

标签: angular angular2-template angular2-directives angular2-components

I have been trying to implement a simple ngFor with Angular2 but I don't know what went wrong which lead to error 'Generic TYpe Array requires one argument(s). PLease favour

import { Component } from '@angular/core';


    @Component({
        selector: 'my-app',
        templateUrl:'./app.component.html',                     
    })
    export class AppComponent { 
           clients:Array;
           doctors:Array;
            constructor(){
               this.clients=["Client1", "Client2", "Client3"];
                this.doctors=["Doctor1","Doctor2","Doctor3"];
            }


    }

2 个答案:

答案 0 :(得分:12)

解决方案1:

clients: String[]; // if type cant be determined use 'any[]'
    doctors: String[];

解决方案2:

clients: Array<String>; // if type cant be determined use '<any>'
    doctors: Array<String>;

答案 1 :(得分:1)

我没有使用Angular2,但我相信解决方案,因为您知道数组将要保留的类型,就是使用@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface LogExecutionTime { } @Aspect @Component public class LogExecutionTimeAspect { private static final Logger logger = LoggerFactory.getLogger(LogExecutionTimeAspect.class); @Around("@annotation(LogExecutionTime)") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { final long start = System.currentTimeMillis(); final Object proceed = joinPoint.proceed(); final long executionTime = System.currentTimeMillis() - start; logger.info(joinPoint.getSignature() + " executed in " + executionTime + "ms"); return proceed; } } public class DummyCallable implements Callable<Integer> { private Integer start, count; DummyCallable() {} DummyCallable(Integer start, Integer count) { this.start = start; this.count = count; } @LogExecutionTime // not working... @Override public Integer call() throws Exception { Thread.sleep(start * 1000); Integer sum = 0; for (Integer i = start; i <= start + count; i++) { sum += i; } return sum; } } @LogExecutionTime // This will work... public List<Integer> getAllUserScores() { Callable c1 = new DummyCallable(1, 100000); Callable c2 = new DummyCallable(2, 100000); Callable c3 = new DummyCallable(3, 100000); // .... run them ... return result; } 而不是数组。

注意:您可以做的是将Array<String>替换为字符串基元的angular2 typename。