ここに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"キーワードを使用しても機能しません。
あなたは 'const'オブジェクトのプロパティを持つことができません。 – Pointy
[クラススコープ内の] TypeScript(https://github.com/Microsoft/TypeScript/issues/12)では、 'const'の代わりに' readonly'を使います。 https://stackoverflow.com/questions/46561155/difference-between-const-and-readonly-in-typescript – artem