Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用vue typescript中的枚举声明全局命名空间_Typescript_Vue.js - Fatal编程技术网

用vue typescript中的枚举声明全局命名空间

用vue typescript中的枚举声明全局命名空间,typescript,vue.js,Typescript,Vue.js,我试图在global.d.ts或global.ts declare namespace Upload { export enum Status { SUCCESS = 'success', ERROR = 'error', ONGOING = 'uploading' } } namespace Upload { export enum Status { SUCCESS = 'success', ERROR = 'error', ONG

我试图在
global.d.ts
global.ts

declare namespace Upload {
  export enum Status {
    SUCCESS = 'success',
    ERROR = 'error',
    ONGOING = 'uploading'
  }
}
namespace Upload {
  export enum Status {
    SUCCESS = 'success',
    ERROR = 'error',
    ONGOING = 'uploading'
  }
}
在my
global.d.ts

declare namespace Upload {
  export enum Status {
    SUCCESS = 'success',
    ERROR = 'error',
    ONGOING = 'uploading'
  }
}
namespace Upload {
  export enum Status {
    SUCCESS = 'success',
    ERROR = 'error',
    ONGOING = 'uploading'
  }
}
或者在
global.ts中

declare namespace Upload {
  export enum Status {
    SUCCESS = 'success',
    ERROR = 'error',
    ONGOING = 'uploading'
  }
}
namespace Upload {
  export enum Status {
    SUCCESS = 'success',
    ERROR = 'error',
    ONGOING = 'uploading'
  }
}
然后在
FileUpload.vue中使用它

import { Component, Vue } from 'vue-property-decorator'

// using it directly without aliasing also not work
import UPLOAD = Upload.Status 

@Component
export default class FileUpload extends Vue {    
  onUploaded(files: object[]) {
    const upload = files[0]
    let status: string
    if (upload.success) status = UPLOAD.SUCCESS
    else if (upload.error) status = UPLOAD.ERROR
    else status = UPLOAD.ONGOING
    this.progress(upload.response, status, Number(upload.progress))
  }
}
在编译/传输时没有错误。然而,在运行时,它给了我一个错误

Uncaught ReferenceError: Upload is not defined
    at eval (eval at ./node_modules/cache-loader/dist/cjs.js?{"cacheDirectory":"/home/wildan/Projects/Sensorfied/EDI/edi-ui/node_modules/.cache/cache-loader"}!./node_modules/babel-loader/lib/index.js!./node_modules/ts-loader/index.js?{"transpileOnly":true,"appendTsSuffixTo":[{}],"happyPackMode":false}!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/components/FileUpload.vue (app.js:836), <anonymous>:37:14)
    at Object../node_modules/cache-loader/dist/cjs.js?{"cacheDirectory":"/home/wildan/Projects/Sensorfied/EDI/edi-ui/node_modules/.cache/cache-loader"}!./node_modules/babel-loader/lib/index.js!./node_modules/ts-loader/index.js?{"transpileOnly":true,"appendTsSuffixTo":[{}],"happyPackMode":false}!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/components/FileUpload.vue (app.js:836)
    at __webpack_require__ (app.js:679)
    at fn (app.js:89)
    at eval (FileUpload.vue?8134:1)
    at Object../src/components/FileUpload.vue (app.js:2281)
    at __webpack_require__ (app.js:679)
    at fn (app.js:89)
    at eval (eval at ./node_modules/cache-loader/dist/cjs.js?{"cacheDirectory":"/home/wildan/Projects/Sensorfied/EDI/edi-ui/node_modules/.cache/cache-loader"}!./node_modules/babel-loader/lib/index.js!./node_modules/ts-loader/index.js?{"transpileOnly":true,"appendTsSuffixTo":[{}],"happyPackMode":false}!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/components/CreateEthingDialog.vue (app.js:828), <anonymous>:13:74)
    at Object../node_modules/cache-loader/dist/cjs.js?{"cacheDirectory":"/home/wildan/Projects/Sensorfied/EDI/edi-ui/node_modules/.cache/cache-loader"}!./node_modules/babel-loader/lib/index.js!./node_modules/ts-loader/index.js?{"transpileOnly":true,"appendTsSuffixTo":[{}],"happyPackMode":false}!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/components/CreateEthingDialog.vue (app.js:828)
但是如果我在
FileUpload.vue
中这样声明它,它就会工作

// tslint:disable-next-line:no-namespace
namespace Upload {
  export enum Status {
    SUCCESS = 'success',
    ERROR = 'error',
    ONGOING = 'uploading'
  }
}
import UPLOAD = Upload.Status

我也尝试使用
export const enum
,但它也不起作用。

确实不能用“const”关键字导出enum,但你是对的,你必须导出enum。可能您错过了FileUpload.vue中的“从'global.ts'导入{Status}”。我在项目中使用enum,但没有名称空间。您解决了问题吗?最后,我将枚举导出为一个模块,而不是在声明文件中声明它。然而,在我的项目结构中,这不是理想的模式,因为它在本地模块中产生了额外的代码行。是否有任何解决方案可以使用typescript实现这一点,我正在尝试在插件中全局使用枚举,如何做到这一点?