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

时间:2018-03-15 08:21:56

标签: typescript vue.js

我试图在global.d.tsglobal.ts

中分隔常量变量

global.d.ts

declare namespace Upload {
  export enum Status {
    SUCCESS = 'success',
    ERROR = 'error',
    ONGOING = 'uploading'
  }
}

global.ts

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)

同时尝试在没有命名空间的情况下声明,但仍然无法正常工作。

declare const enum Status
// declare enum Status
{
  SUCCESS = 'success',
  ERROR = 'error',
  ONGOING = 'uploading',
  SKIP = 'skip'
}

但是,如果我在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但它也无效。

0 个答案:

没有答案