javascript Create.vue - 脚本部分
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Create.vue - 脚本部分相关的知识,希望对你有一定的参考价值。
<script>
import rules from '@/utils/rules'
export default {
created () {
this.rules = rules
},
data: () => {
return {
isLoading: false,
form: {
name: 'Madeira',
shortDesc: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
fullDesc: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.',
link: 'https://www.lipsum.com/',
tags: ['Developer Tools',
'Web Apps',
'Productivity'
]
},
valid: true,
mainImg: {
size: 0,
name: '',
type: '',
base64: ''
},
tagList: [
'Developer Tools',
'Web Apps',
'Productivity',
'IoT',
'Marketing',
'Hacking',
'Books'
]
}
},
methods: {
changeFile (ev) {
console.log(ev)
if (ev.target.files.length > 0) {
const file = ev.target.files[ 0 ]
this.form.imgName = file.name
const reader = new FileReader()
reader.addEventListener('load', () => {
this.mainImg.name = file.name
this.mainImg.size = file.size
this.mainImg.type = file.type
this.mainImg.base64 = reader.result
}, false)
reader.readAsDataURL(file)
} else {
this.clearImage()
}
},
clearImage () {
this.$refs.inputFile.value = ''
this.mainImg.size = 0
this.mainImg.name = ''
this.mainImg.type = ''
this.mainImg.base64 = ''
},
async submitForm () {
this.isLoading = true
const res = await this.$store.dispatch('createDocRef')
const docId = res.id
const promises = []
const resourceData = {
id: docId,
...this.form,
createdAt: new Date(),
media: {
mainImg: ''
},
favsCount: 0,
likesCount: 0
}
// promise 0
promises.push(this.$store.dispatch('createResource', resourceData))
if (this.$refs.inputFile.files && this.$refs.inputFile.files[0]) {
const imgData = {
id: docId,
file: this.$refs.inputFile.files[0]
}
// promise 1
promises.push(this.$store.dispatch('uploadResourceImg', imgData))
}
try {
await promises[0]
const snapshot = await promises[1]
// Comment this code if you have cloud function backend
const downloadURL = await snapshot.ref.getDownloadURL()
await this.$store.dispatch('createResource', {
id: docId,
media: {
mainImg: downloadURL
}
})
// **************
this.$router.push({ name: 'home' })
} catch (error) {
console.log({ error })
}
},
validateForm () {
if (this.$refs.form.validate()) {
this.submitForm()
}
}
}
}
</script>
以上是关于javascript Create.vue - 脚本部分的主要内容,如果未能解决你的问题,请参考以下文章