Javascript 无法设置属性';当前状态';未定义的使用;这";
我试图在vue js中显示formSubmit的状态。问题是我不太熟悉使用Javascript 无法设置属性';当前状态';未定义的使用;这";,javascript,vue.js,this,Javascript,Vue.js,This,我试图在vue js中显示formSubmit的状态。问题是我不太熟悉使用“this”,现在我在尝试使用“this.currentStatus”声明状态时,代码中的某些地方出现了错误。 这是我的代码: const STATUS_INITIAL = 0 const STATUS_SAVING = 1 const STATUS_SUCCESS = 2 const STATUS_FAILED = 3 export default { name: 'Dashboard',
“this”
,现在我在尝试使用“this.currentStatus”声明状态时,代码中的某些地方出现了错误。
这是我的代码:
const STATUS_INITIAL = 0
const STATUS_SAVING = 1
const STATUS_SUCCESS = 2
const STATUS_FAILED = 3
export default {
name: 'Dashboard',
data () {
return {
file: '',
uploadError: null,
currentStatus: null,
uploadFieldName: 'photos'
}
},
computed: {
clientId () {
return parseInt(this.$route.params.id)
},
isInitial () {
return this.currentStatus === STATUS_INITIAL
},
isSaving () {
return this.currentStatus === STATUS_SAVING
},
isSuccess () {
return this.currentStatus === STATUS_SUCCESS
},
isFailed () {
return this.currentStatus === STATUS_FAILED
}
},
methods: {
handleFileUpload () {
this.file = this.$refs.file.files[0]
console.log(this.file)
},
filesChange (fieldName, fileList) {
// handle file changes
const formData = new FormData()
// append the files to FormData
Array
.from(Array(fileList.length).keys())
.map(x => {
formData.append(fieldName, fileList[x], fileList[x].name)
})
// save it
this.submitFile(formData)
},
submitFile (formData) {
this.currentStatus = STATUS_SAVING
console.log(this.currentStatus)
var clientId = this.clientId
var reader = new FileReader()
reader.readAsDataURL(this.file)
reader.onload = function () {
var asim = reader.result
formData.append('file', this.file)
let promises = []
promises.push(
performRpcWithPromise('insertContract', [
asim, clientId
]).then(function (res) {
console.log(res)
this.currentStatus = STATUS_SUCCESS
console.log(this.currentStatus)
})
)
}
}
}
}
<p v-if="isSuccess">
DONE
</p>
{{currentStatus}}
<form enctype="multipart/form-data" novalidate>
<input type="file" placeholder="Velg en fil" id="file" ref="file" v-on:change="handleFileUpload()"
accept="application/pdf" class="input-file" @change="filesChange($event.target.name, $event.target.files); fileCount = $event.target.files.length">
<p v-if="isSuccess">
wow
</p>
<p v-if="isSaving">
Uploading {{ fileCount }} files...
</p>
</form>
这是我的表格:
const STATUS_INITIAL = 0
const STATUS_SAVING = 1
const STATUS_SUCCESS = 2
const STATUS_FAILED = 3
export default {
name: 'Dashboard',
data () {
return {
file: '',
uploadError: null,
currentStatus: null,
uploadFieldName: 'photos'
}
},
computed: {
clientId () {
return parseInt(this.$route.params.id)
},
isInitial () {
return this.currentStatus === STATUS_INITIAL
},
isSaving () {
return this.currentStatus === STATUS_SAVING
},
isSuccess () {
return this.currentStatus === STATUS_SUCCESS
},
isFailed () {
return this.currentStatus === STATUS_FAILED
}
},
methods: {
handleFileUpload () {
this.file = this.$refs.file.files[0]
console.log(this.file)
},
filesChange (fieldName, fileList) {
// handle file changes
const formData = new FormData()
// append the files to FormData
Array
.from(Array(fileList.length).keys())
.map(x => {
formData.append(fieldName, fileList[x], fileList[x].name)
})
// save it
this.submitFile(formData)
},
submitFile (formData) {
this.currentStatus = STATUS_SAVING
console.log(this.currentStatus)
var clientId = this.clientId
var reader = new FileReader()
reader.readAsDataURL(this.file)
reader.onload = function () {
var asim = reader.result
formData.append('file', this.file)
let promises = []
promises.push(
performRpcWithPromise('insertContract', [
asim, clientId
]).then(function (res) {
console.log(res)
this.currentStatus = STATUS_SUCCESS
console.log(this.currentStatus)
})
)
}
}
}
}
<p v-if="isSuccess">
DONE
</p>
{{currentStatus}}
<form enctype="multipart/form-data" novalidate>
<input type="file" placeholder="Velg en fil" id="file" ref="file" v-on:change="handleFileUpload()"
accept="application/pdf" class="input-file" @change="filesChange($event.target.name, $event.target.files); fileCount = $event.target.files.length">
<p v-if="isSuccess">
wow
</p>
<p v-if="isSaving">
Uploading {{ fileCount }} files...
</p>
</form>
这对我来说有点奇怪,因为this.currentStatus=STATUS\u SAVING
正在工作并显示数字“1”,但不在承诺(.then)内
任何人都可以看到为什么这不是在承诺内部工作,而是在承诺外部工作?您可以使用箭头函数或闭包:
var self = this
reader.onload = function () {
var asim = reader.result
formData.append('file', self.file)
let promises = []
promises.push(
performRpcWithPromise('insertContract', [
asim, clientId
]).then(function (res) {
console.log(res)
self.currentStatus = STATUS_SUCCESS
console.log(self.currentStatus)
})
)
}
如果您想使用箭头功能,请尝试
reader.onload = () => {
var asim = reader.result
formData.append('file', this.file)
let promises = []
promises.push(
performRpcWithPromise('insertContract', [
asim, clientId
]).then((res) => {
console.log(res)
this.currentStatus = STATUS_SUCCESS
console.log(this.currentStatus)
})
)
}
尝试改用箭头函数。像这样:
.then(res => {
console.log(res)
this.currentStatus = STATUS_SUCCESS
console.log(this.currentStatus)
})
我相信这与。awsome@Thammarith类似。但是状态没有改变,它现在是console.log(this.currentStatus)=2,但是v-if=“issucess”没有显示?@AliDurrani我正在查看它。同时,您能否尝试在
部分中打印{currentStatus}
和{{issucess}
?因此,您可以查看是否实际更改了currentStatus
。这也更新了DOM。为什么不使用箭头功能