2017-10-05 3 views
0

ここにtypecriptの範囲に関する質問があります。クラスの閉じ括弧の外にそれならばlistofstuff定数作品ではなく、それが例えばブラケットなぜ、constはtypescriptのエクスポートクラスの外になければなりませんか?

内にある場合、このコードは動作しません。

import {Injectable} from '@angular/core' 

    @Injectable() 
    export class EventListService{ 
    getEvents(){ 
    return listofstuff 
    } 
const listofstuff = [ 
    {name:'Angular Connect', date: '9/26/2036', time: '10am', location: {address: '1 London Rd', city: 'London', country: 'England'}}, 
    {name:'ng-nl', date: '4/15/2037', time: '9am', location: {address: '127 DT ', city: 'Amsterdam', country: 'NL'}}, 
    {name:'ng-conf 2037', date: '4/15/2037', time: '9am', location: {address: 'The Palatial America Hotel', city: 'Salt Lake City', country: 'USA'}}, 
    {name:'UN Angular Summit', date: '6/10/2037', time: '8am', location: {address: 'The UN Angular Center', city: 'New York', country: 'USA'}}, 
] 
} 

しかし、これは動作します:

import {Injectable} from '@angular/core' 

    @Injectable() 
    export class EventListService{ 
    getEvents(){ 
    return listofstuff 
    } 
} 
const listofstuff = [ 
    {name:'Angular Connect', date: '9/26/2036', time: '10am', location: {address: '1 London Rd', city: 'London', country: 'England'}}, 
    {name:'ng-nl', date: '4/15/2037', time: '9am', location: {address: '127 DT ', city: 'Amsterdam', country: 'NL'}}, 
    {name:'ng-conf 2037', date: '4/15/2037', time: '9am', location: {address: 'The Palatial America Hotel', city: 'Salt Lake City', country: 'USA'}}, 
    {name:'UN Angular Summit', date: '6/10/2037', time: '8am', location: {address: 'The UN Angular Center', city: 'New York', country: 'USA'}}, 
] 

オブジェクト指向のバックグラウンド(C#とjava)から来ていますが、これは私にとっては奇妙なことです。誰かがここで何が起こっているのか説明できますか?最初の例で "this"キーワードを使用しても機能しません。

+0

あなたは 'const'オブジェクトのプロパティを持つことができません。 – Pointy

+0

[クラススコープ内の] TypeScript(https://github.com/Microsoft/TypeScript/issues/12)では、 'const'の代わりに' readonly'を使います。 https://stackoverflow.com/questions/46561155/difference-between-const-and-readonly-in-typescript – artem

答えて

3

クラスプロパティにはconstキーワードを使用できません。代わりに、クラスプロパティはpublic,privatereadonlyおよびprotectedの修飾子でのみマークすることができます。

import { Injectable } from '@angular/core' 

@Injectable() 
export class EventListService { 
    readonly listofstuff: any[] = [ 
    { name: 'Angular Connect', date: '9/26/2036', time: '10am', location: { address: '1 London Rd', city: 'London', country: 'England' } }, 
    { name: 'ng-nl', date: '4/15/2037', time: '9am', location: { address: '127 DT ', city: 'Amsterdam', country: 'NL' } }, 
    { name: 'ng-conf 2037', date: '4/15/2037', time: '9am', location: { address: 'The Palatial America Hotel', city: 'Salt Lake City', country: 'USA' } }, 
    { name: 'UN Angular Summit', date: '6/10/2037', time: '8am', location: { address: 'The UN Angular Center', city: 'New York', country: 'USA' } }, 
    ]; 

    getEvents() { 
    return this.listofstuff; 
    } 
} 

あなたはthisキーワードを使用してlistofstuffクラスのプロパティにアクセスすると思います。 Example

Typescriptクラスの詳細については、documentationでお読みください。識別子なしのプロパティとメンバは、デフォルトでpublicとマークされています。ドキュメントから:

各メンバーは、既定ではパブリックです。

うまくいけば助けてください!

+0

非常に参考になります。ありがとうございました! –

関連する問題