[英]Why does a const have to be outside of the export class in typescript?
Here is a question relating to scope in typescript. 这是一个与打字稿范围有关的问题。 The listofstuff constant works if it outside of the class closing bracket, but not if it is inside the bracket
如果listofstuff常量在类关闭括号之外,则起作用,但在括号内则不起作用
for instance, this code does not work: 例如,此代码不起作用:
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'}},
]
}
But this works: 但这有效:
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'}},
]
Coming from an object oriented background (C# and some java), this is strange to me. 来自面向对象的背景(C#和某些Java),这对我来说很奇怪。 can someone explain what is going on here?
有人可以解释这里发生了什么吗? Even using the "this" keyword in the first example does not work...
即使在第一个示例中使用“ this”关键字也不起作用...
You cannot use the const
keyword for class properties. 您不能将
const
关键字用于类属性。 Instead class properties can only be marked with public
, private
, readonly
and protected
modifiers. 相反,只能使用
public
, private
, readonly
和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;
}
}
You'd access the listofstuff
class property using the this
keyword. 您将使用
this
关键字访问listofstuff
类属性。 Example 例
You can read more about Typescript classes in the official documentation . 您可以在官方文档中阅读有关Typescript类的更多信息。 Keep in mind properties and members without an identifier are marked as
public
by default. 请记住,默认情况下,属性和没有标识符的成员被标记为
public
。 From the documentation: 从文档中:
In TypeScript, each member is public by default.
在TypeScript中,默认情况下每个成员都是公共的。
Hopefully that helps! 希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.