我在使头像选择器工作时遇到一些问题...
单击以选择未替换的头像,并出现错误 TypeError:无法读取VueComponent.selectAvatar
中未定义的属性'src'我目前正在使用带有v-for的Vuetify和v-avatar
组件来加载所有图像。
有什么主意吗?
HTML
<v-flex xs12 pt-0 pb-0>
<h1 class="title mb-4">User Details</h1>
<v-avatar
size="100px"
>
<img
:src="this.selectedAvatar"
alt="Avatar"
>
</v-avatar>
</v-flex>
<v-flex x12>
<v-btn
color="primary"
flat="flat"
small
@click="selectAvatarDialog = true"
class="avatar-btn"
>
Update avatar
</v-btn>
</v-flex>
<v-dialog
v-model="selectAvatarDialog"
max-width="80%"
>
<v-card>
<v-container fluid pa-2>
<v-layout row wrap align-center justify-center fill-height>
<v-flex xs6 sm4 md3 lg2 my-2 class="text-xs-center"
v-for="(avatar,i) in avatars"
:key="i">
<v-img
:src="avatar.src"
aspect-ratio="1"
width="100px"
max-width="100px"
min-width="100px"
class="dialog-avatar-img"
@click="selectAvatar()"
></v-img>
</v-flex>
</v-layout>
<v-card-actions class="mt-2">
<v-spacer></v-spacer>
<v-btn
color="primary"
flat="flat"
@click="selectAvatarDialog = false"
>
Cancel
</v-btn>
</v-card-actions>
</v-container>
</v-card>
</v-dialog>
JS
export default {
layout: 'default',
data() {
return {
selectAvatarDialog: false,
avatars: [
{src: require('@/assets/images/avatar-01.png') },
{ src: require('@/assets/images/avatar-02.png') },
{ src: require('@/assets/images/avatar-03.png') },
{ src: require('@/assets/images/avatar-04.png') },
{ src: require('@/assets/images/avatar-05.png') }
],
selectedAvatar: require('@/assets/images/avatar-01.png'),
}
},
methods: {
selectAvatar(){
this.selectedAvatar = this.avatar.src
console.log('Avatar selected')
}
}
}
谢谢
答案 0 :(得分:1)
问题出在您试图使用'this.avatar'的selectAvatar方法中,但它不存在。 for循环中的化身不会传递到脚本中。您应该这样做:
<v-img
...
@click="selectAvatar(i)"
></v-img>
在您的脚本中:
methods: {
selectAvatar(i){
this.selectedAvatar = this.avatars[i].src
console.log('Avatar selected')
}
}