'this' 不是代码拆分中的函数
'this' is not a function in code splitting
我不知道如何处理我代码中'this'的问题。
在我使用代码拆分之前一切正常。
我正在尝试在单击按钮后导入组件。所以我删除了 componentWillMount。
代码拆分前:
import React, {PureComponent} from 'react';
import Uppy from 'uppy/lib/core';
class Shopper extends PureComponent {
constructor (props) {
super(props); }
componentWillMount () {
this.uppy2 = new Uppy({
autoProceed: false,
onBeforeUpload: (files) => {
const updatedFiles = Object.assign({}, files)
Object.keys(updatedFiles).forEach(fileId => {
let origName = updatedFiles[fileId].name;
let metaName = updatedFiles[fileId].meta.name;
/* some code */
if (fileDir) {
this.uppy2.setFileMeta(uploadId, { name: `${addBeforeName}-${metaName}.${updatedFiles[fileId].extension}` })
/* some more codes */
} else {
/* some more code */
}
})
return updatedFiles
}
})
代码拆分后:
import React, {PureComponent} from 'react';
class Shopper extends PureComponent {
constructor (props) {
super(props);
this.uploadModal = this.uploadModal.bind(this);
}
uploadModal () {
this.uppy2 = import('uppy/lib/core')
.then((uppydata) => new uppydata({
autoProceed: false,
onBeforeUpload: (files) => {
const updatedFiles = Object.assign({}, files)
Object.keys(updatedFiles).forEach(fileId => {
let origName = updatedFiles[fileId].name;
let metaName = updatedFiles[fileId].meta.name;
if (fileDir) {
this.uppy2.setFileMeta(uploadId, { name: `${addBeforeName}-${metaName}.${updatedFiles[fileId].extension}` })
/* Here this.uppy2.setFileMeta is giving error */
} else {
/ * some more code */
}
})
return updatedFiles
}
})
)
.then(useTus => {
/* some more codes */
})
代码拆分后出现此错误:
Uncaught TypeError: _this2.uppy2.setFileMeta is not a function
您正在将 uppy2 分配给承诺,而不是 class 实例:
import('uppy/lib/core').then((uppydata) => {
this.uppy2 = new uppydata({
autoProceed: false,
onBeforeUpload: (files) => {
const updatedFiles = Object.assign({}, files)
Object.keys(updatedFiles).forEach(fileId => {
let origName = updatedFiles[fileId].name;
let metaName = updatedFiles[fileId].meta.name;
if (fileDir) {
this.uppy2.setFileMeta(uploadId, {
name: `${addBeforeName}-${metaName}.${updatedFiles[fileId].extension}`
})
// ...
我不知道如何处理我代码中'this'的问题。 在我使用代码拆分之前一切正常。
我正在尝试在单击按钮后导入组件。所以我删除了 componentWillMount。
代码拆分前:
import React, {PureComponent} from 'react';
import Uppy from 'uppy/lib/core';
class Shopper extends PureComponent {
constructor (props) {
super(props); }
componentWillMount () {
this.uppy2 = new Uppy({
autoProceed: false,
onBeforeUpload: (files) => {
const updatedFiles = Object.assign({}, files)
Object.keys(updatedFiles).forEach(fileId => {
let origName = updatedFiles[fileId].name;
let metaName = updatedFiles[fileId].meta.name;
/* some code */
if (fileDir) {
this.uppy2.setFileMeta(uploadId, { name: `${addBeforeName}-${metaName}.${updatedFiles[fileId].extension}` })
/* some more codes */
} else {
/* some more code */
}
})
return updatedFiles
}
})
代码拆分后:
import React, {PureComponent} from 'react';
class Shopper extends PureComponent {
constructor (props) {
super(props);
this.uploadModal = this.uploadModal.bind(this);
}
uploadModal () {
this.uppy2 = import('uppy/lib/core')
.then((uppydata) => new uppydata({
autoProceed: false,
onBeforeUpload: (files) => {
const updatedFiles = Object.assign({}, files)
Object.keys(updatedFiles).forEach(fileId => {
let origName = updatedFiles[fileId].name;
let metaName = updatedFiles[fileId].meta.name;
if (fileDir) {
this.uppy2.setFileMeta(uploadId, { name: `${addBeforeName}-${metaName}.${updatedFiles[fileId].extension}` })
/* Here this.uppy2.setFileMeta is giving error */
} else {
/ * some more code */
}
})
return updatedFiles
}
})
)
.then(useTus => {
/* some more codes */
})
代码拆分后出现此错误:
Uncaught TypeError: _this2.uppy2.setFileMeta is not a function
您正在将 uppy2 分配给承诺,而不是 class 实例:
import('uppy/lib/core').then((uppydata) => {
this.uppy2 = new uppydata({
autoProceed: false,
onBeforeUpload: (files) => {
const updatedFiles = Object.assign({}, files)
Object.keys(updatedFiles).forEach(fileId => {
let origName = updatedFiles[fileId].name;
let metaName = updatedFiles[fileId].meta.name;
if (fileDir) {
this.uppy2.setFileMeta(uploadId, {
name: `${addBeforeName}-${metaName}.${updatedFiles[fileId].extension}`
})
// ...