简体   繁体   English

为什么const必须在typescript中的export类之外?

[英]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. 相反,只能使用publicprivatereadonlyprotected修饰符标记类属性。

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.

相关问题 使用 typescript 中的方法导出从 class 派生的 const object - Export const object deriving from class with methods in typescript 为什么这在 TypeScript 中是不可能的 `export { add } from './math';` `const math = { add };` - Why this is impossible in TypeScript `export { add } from './math';` `const math = { add };` 打字稿const导出和导入不起作用? - typescript const export and import is not working? 为什么无法在TypeScript中导出类实例? - Why is it not possible to export a class instance in TypeScript? TypeScript如何在Promise中导出const - TypeScript how to export const in a promise 为什么 TypeScript 不支持导出@deco? - Why does TypeScript not support export @deco? 当我们动态导入没有默认导出的 javascript 文件时,为什么 typescript 会推断出默认道具 - Why does typescript infer a default prop when we dynamically import a javascript file which does not have a default export Typescript和Uncaught SyntaxError:在严格模式之外尚不支持块范围的声明(let,const,function,class) - Typescript and Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode 在类外反应绑定到const - React Bind to const outside Class Typescript的/ ** @class * /有目的吗? - Does Typescript's /** @class */ have a purpose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM