运行Angular2 ver.2.0.0-alpha.44

时间:2015-11-06 20:51:37

标签: typescript angular

Angular2的新手,试图让this plunkr项目在我的本地系统上运行。我收到以下错误:

错误:(25,23)TS2339:财产'人员'类型' PeopleService'中不存在。   错误:(26,39)TS2339:财产'人员'类型'人物'

上不存在

就行了:             peopleService.people
                .subscribe(people => this.people = people);

我有理由相信我已正确设置了环境,因为我正在运行其他基本组件。

people.ts 档案:

import {Component, View, CORE_DIRECTIVES} from 'angular2/angular2'  
import {PeopleService} from './peopleService'  
import {Person} from './person'

@Component({  
    selector: 'my-app',  
    providers: [PeopleService]  
})  
@View({
    template: `
    <div>
      <h2>Hello Angular2!</h2>
      <my-person
        *ng-for="#person of people"
        [name]="person.name"
        (hello)="saidHello($event)">
      </my-person>
    </div>
  `,  
        directives: [CORE_DIRECTIVES, Person]  
})  
export class People {  
    constructor(public peopleService:PeopleService) {  
        peopleService.people  
            .subscribe(people => this.people = people);  
    }  
    saidHello($event){  
        alert(`You said hello to ${$event}`)  
    }
}

peopleService.ts 文件中的类似错误:

错误:(8,14)TS2339:财产&#39;人员&#39;类型&#39; PeopleService&#39;。

上不存在
//a simple service
import {Injectable} from 'angular2/angular2';
import {Http} from 'angular2/http';

@Injectable()
export class PeopleService {
  constructor(http:Http) {
    this.people = http.get('api/people.json').map(res => res.json());
  }
}

非常感谢任何指导!

Yaniv, I'm still working thru this, below is the exception after implementing the change.  Also, I didn't notice this before but I'm getting a warning in the people.ts file on the following statements:
    [name]="person.name"
    (hello)="saidHello($event)">

警告    此处不允许使用属性[名称]
   这里不允许属性(你好)

EXCEPTION: Cannot resolve all parameters for People(?). Make sure they all have valid type or annotations.
angular2.dev.js:21835 EXCEPTION: Cannot resolve all parameters for People(?). Make sure they all have valid type or annotations.BrowserDomAdapter.logError @ angular2.dev.js:21835BrowserDomAdapter.logGroup @ angular2.dev.js:21846ExceptionHandler.call @ angular2.dev.js:4431(anonymous function) @ angular2.dev.js:19543NgZone._onError @ angular2.dev.js:10711errorHandling.onError @ angular2.dev.js:10630run @ angular2.dev.js:141(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$$internal$$tryCatch @ angular2.dev.js:1507lib$es6$promise$$internal$$invokeCallback @ angular2.dev.js:1519lib$es6$promise$$internal$$publish @ angular2.dev.js:1490(anonymous function) @ angular2.dev.js:219microtask @ angular2.dev.js:10670run @ angular2.dev.js:138(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$asap$$flush @ angular2.dev.js:1301
angular2.dev.js:21835 STACKTRACE:BrowserDomAdapter.logError @ angular2.dev.js:21835ExceptionHandler.call @ angular2.dev.js:4433(anonymous function) @ angular2.dev.js:19543NgZone._onError @ angular2.dev.js:10711errorHandling.onError @ angular2.dev.js:10630run @ angular2.dev.js:141(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$$internal$$tryCatch @ angular2.dev.js:1507lib$es6$promise$$internal$$invokeCallback @ angular2.dev.js:1519lib$es6$promise$$internal$$publish @ angular2.dev.js:1490(anonymous function) @ angular2.dev.js:219microtask @ angular2.dev.js:10670run @ angular2.dev.js:138(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$asap$$flush @ angular2.dev.js:1301
angular2.dev.js:21835 Error: Cannot resolve all parameters for People(?). Make sure they all have valid type or annotations.
    at NoAnnotationError.BaseException [as constructor] (http://127.0.0.1:8080/node_modules/angular2/bundles/angular2.dev.js:16034:21)
    at new NoAnnotationError (http://127.0.0.1:8080/node_modules/angular2/bundles/angular2.dev.js:4983:14)
    at _dependenciesFor (http://127.0.0.1:8080/node_modules/angular2/bundles/angular2.dev.js:20066:13)
    at resolveFactory (http://127.0.0.1:8080/node_modules/angular2/bundles/angular2.dev.js:19964:22)
    at Object.resolveProvider (http://127.0.0.1:8080/node_modules/angular2/bundles/angular2.dev.js:19983:66)
    at Function.DirectiveProvider.createFromProvider (http://127.0.0.1:8080/node_modules/angular2/bundles/angular2.dev.js:23906:27)
    at Function.DirectiveProvider.createFromType (http://127.0.0.1:8080/node_modules/angular2/bundles/angular2.dev.js:23915:32)
    at provideDirective (http://127.0.0.1:8080/node_modules/angular2/bundles/angular2.dev.js:27209:49)
    at http://127.0.0.1:8080/node_modules/angular2/bundles/angular2.dev.js:27188:14
    at Array.map (native)BrowserDomAdapter.logError @ angular2.dev.js:21835ExceptionHandler.call @ angular2.dev.js:4434(anonymous function) @ angular2.dev.js:19543NgZone._onError @ angular2.dev.js:10711errorHandling.onError @ angular2.dev.js:10630run @ angular2.dev.js:141(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$$internal$$tryCatch @ angular2.dev.js:1507lib$es6$promise$$internal$$invokeCallback @ angular2.dev.js:1519lib$es6$promise$$internal$$publish @ angular2.dev.js:1490(anonymous function) @ angular2.dev.js:219microtask @ angular2.dev.js:10670run @ angular2.dev.js:138(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$asap$$flush @ angular2.dev.js:1301
angular2.dev.js:21845 EXCEPTION: TypeError: Cannot read property 'location' of undefined
angular2.dev.js:21835 EXCEPTION: TypeError: Cannot read property 'location' of undefinedBrowserDomAdapter.logError @ angular2.dev.js:21835BrowserDomAdapter.logGroup @ angular2.dev.js:21846ExceptionHandler.call @ angular2.dev.js:4431(anonymous function) @ angular2.dev.js:19543NgZone._onError @ angular2.dev.js:10711errorHandling.onError @ angular2.dev.js:10630run @ angular2.dev.js:141(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$$internal$$tryCatch @ angular2.dev.js:1507lib$es6$promise$$internal$$invokeCallback @ angular2.dev.js:1519lib$es6$promise$$internal$$publish @ angular2.dev.js:1490(anonymous function) @ angular2.dev.js:219microtask @ angular2.dev.js:10670run @ angular2.dev.js:138(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$asap$$flush @ angular2.dev.js:1301
angular2.dev.js:21835 STACKTRACE:BrowserDomAdapter.logError @ angular2.dev.js:21835ExceptionHandler.call @ angular2.dev.js:4433(anonymous function) @ angular2.dev.js:19543NgZone._onError @ angular2.dev.js:10711errorHandling.onError @ angular2.dev.js:10630run @ angular2.dev.js:141(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$$internal$$tryCatch @ angular2.dev.js:1507lib$es6$promise$$internal$$invokeCallback @ angular2.dev.js:1519lib$es6$promise$$internal$$publish @ angular2.dev.js:1490(anonymous function) @ angular2.dev.js:219microtask @ angular2.dev.js:10670run @ angular2.dev.js:138(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$asap$$flush @ angular2.dev.js:1301
angular2.dev.js:21835 TypeError: Cannot read property 'location' of undefined
    at angular2.dev.js:19434
    at Zone.run (angular2.dev.js:138)
    at Zone.run (angular2.dev.js:10644)
    at zoneBoundFn (angular2.dev.js:111)
    at lib$es6$promise$$internal$$tryCatch (angular2.dev.js:1507)
    at lib$es6$promise$$internal$$invokeCallback (angular2.dev.js:1519)
    at lib$es6$promise$$internal$$publish (angular2.dev.js:1490)
    at angular2.dev.js:219
    at microtask (angular2.dev.js:10670)
    at Zone.run (angular2.dev.js:138)BrowserDomAdapter.logError @ angular2.dev.js:21835ExceptionHandler.call @ angular2.dev.js:4434(anonymous function) @ angular2.dev.js:19543NgZone._onError @ angular2.dev.js:10711errorHandling.onError @ angular2.dev.js:10630run @ angular2.dev.js:141(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$$internal$$tryCatch @ angular2.dev.js:1507lib$es6$promise$$internal$$invokeCallback @ angular2.dev.js:1519lib$es6$promise$$internal$$publish @ angular2.dev.js:1490(anonymous function) @ angular2.dev.js:219microtask @ angular2.dev.js:10670run @ angular2.dev.js:138(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$asap$$flush @ angular2.dev.js:1301
angular2.dev.js:21845 EXCEPTION: TypeError: Cannot read property 'hostView' of undefined
angular2.dev.js:21835 EXCEPTION: TypeError: Cannot read property 'hostView' of undefinedBrowserDomAdapter.logError @ angular2.dev.js:21835BrowserDomAdapter.logGroup @ angular2.dev.js:21846ExceptionHandler.call @ angular2.dev.js:4431(anonymous function) @ angular2.dev.js:19543NgZone._onError @ angular2.dev.js:10711errorHandling.onError @ angular2.dev.js:10630run @ angular2.dev.js:141(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$$internal$$tryCatch @ angular2.dev.js:1507lib$es6$promise$$internal$$invokeCallback @ angular2.dev.js:1519lib$es6$promise$$internal$$publish @ angular2.dev.js:1490(anonymous function) @ angular2.dev.js:219microtask @ angular2.dev.js:10670run @ angular2.dev.js:138(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$asap$$flush @ angular2.dev.js:1301
angular2.dev.js:21835 STACKTRACE:BrowserDomAdapter.logError @ angular2.dev.js:21835ExceptionHandler.call @ angular2.dev.js:4433(anonymous function) @ angular2.dev.js:19543NgZone._onError @ angular2.dev.js:10711errorHandling.onError @ angular2.dev.js:10630run @ angular2.dev.js:141(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$$internal$$tryCatch @ angular2.dev.js:1507lib$es6$promise$$internal$$invokeCallback @ angular2.dev.js:1519lib$es6$promise$$internal$$publish @ angular2.dev.js:1490(anonymous function) @ angular2.dev.js:219microtask @ angular2.dev.js:10670run @ angular2.dev.js:138(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$asap$$flush @ angular2.dev.js:1301
angular2.dev.js:21835 TypeError: Cannot read property 'hostView' of undefined
    at tick (angular2.dev.js:19626)
    at Zone.run (angular2.dev.js:138)
    at Zone.run (angular2.dev.js:10644)
    at zoneBoundFn (angular2.dev.js:111)
    at lib$es6$promise$$internal$$tryCatch (angular2.dev.js:1507)
    at lib$es6$promise$$internal$$invokeCallback (angular2.dev.js:1519)
    at lib$es6$promise$$internal$$publish (angular2.dev.js:1490)
    at angular2.dev.js:219
    at microtask (angular2.dev.js:10670)
    at Zone.run (angular2.dev.js:138)BrowserDomAdapter.logError @ angular2.dev.js:21835ExceptionHandler.call @ angular2.dev.js:4434(anonymous function) @ angular2.dev.js:19543NgZone._onError @ angular2.dev.js:10711errorHandling.onError @ angular2.dev.js:10630run @ angular2.dev.js:141(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$$internal$$tryCatch @ angular2.dev.js:1507lib$es6$promise$$internal$$invokeCallback @ angular2.dev.js:1519lib$es6$promise$$internal$$publish @ angular2.dev.js:1490(anonymous function) @ angular2.dev.js:219microtask @ angular2.dev.js:10670run @ angular2.dev.js:138(anonymous function) @ angular2.dev.js:10644zoneBoundFn @ angular2.dev.js:111lib$es6$promise$asap$$flush @ angular2.dev.js:1301

修改

PeopleService的简化版本

//a simple service import {Injectable} from 'angular2/angular2';

@Injectable() export class PeopleService {
    people: any;
    constructor() {
        this.people = [
            {"id": 1, "name": "Brad"},
            {"id": 2, "name": "Jules"},
            {"id": 3, "name": "Jeff"}
        ]
    } }

3 个答案:

答案 0 :(得分:11)

错误是由Typescript编译器引起的。您必须将people定义为PeopleService的成员:

 //a simple service
import {Injectable} from 'angular2/angular2';
import {Http} from 'angular2/http';

@Injectable()
export class PeopleService {
  people: any;//-> this part is missing in your code.
  constructor(http:Http) {
    this.people = http.get('api/people.json').map(res => res.json());
  }
}

我不确定它是什么类型的数组,所以我做了any。无论如何,它应该被定义为一个类成员。

修改

响应您在PeopleService中使用错误进行编辑。尝试更新您的组件以注入像这样的People服务:

import {Component, View, CORE_DIRECTIVES} from 'angular2/angular2'
import {Inject} from 'angular2/core'; 
import {PeopleService} from './peopleService'  
import {Person} from './person'

@Component({  
    selector: 'my-app',  
    providers: [PeopleService]  
})  
@View({
    template: `
    <div>
      <h2>Hello Angular2!</h2>
      <my-person
        *ng-for="#person of people"
        [name]="person.name"
        (hello)="saidHello($event)">
      </my-person>
    </div>
  `,  
        directives: [CORE_DIRECTIVES, Person]  
})  
export class People {
    public peopleService: PeopleService;   

    constructor(@Inject(PeopleService) peopleService:PeopleService) {  
      this.peopleService = peopleService;  
      this.peopleService.people  
            .subscribe(people => this.people = people);  
    }  
    saidHello($event){  
        alert(`You said hello to ${$event}`)  
    }
}

注意构造函数中的@Inject(PeopleService)及其新导入:

import {Inject} from 'angular2/core';

我建议在Angular 2中阅读关于依赖注入的awesome post,这对我帮助很大!

答案 1 :(得分:0)

@Yaniv,这是new plunkr

与之前的参考plunkr一样,这个没有出现错误。例外情况仅发生在使用tsc和live-server的本地环境中。

我发现在我的本地环境中,异常将根据我最后触摸的文件(无功能更改)而来。例如,如果我触及peopleservice.ts(并保存),该应用程序将正常工作,但如果我触及people.ts ....我得到例外。

我修改了系统配置(config.js),添加了一个地图定义并将包defaultextension更改为ts。这似乎对加载器产生了积极影响,因为现在我从未在本地环境中获得异常。应用程序运行正常,就像在plunkr环境中一样。

select

除非根据您的建议将人员添加为成员,否则Webstorm IDE仍会在people.ts和peopleservice.ts中突出显示“属性人员不存在类型...”错误。将人员添加为成员会消除IDE错误,但它似乎不会对执行产生影响。

答案 2 :(得分:-3)

定义构造函数时,将服务的实例设为私有而不是公共。 希望这会奏效。