gpt4 book ai didi

javascript - 使用 Nuxt 和 Vuetify 的头像选择器

转载 作者:行者123 更新时间:2023-12-02 23:44:31 24 4
gpt4 key购买 nike

我在使头像选择器正常工作时遇到一些问题...

点击选择头像后,头像未被替换,并出现错误TypeError: Cannot read property 'src' of undefined at VueComponent.selectAvatar

我目前正在使用 Vuetify 和带有 v-for 的 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')
}
}
}

谢谢

最佳答案

问题出在您的 selectAvatar 方法中,您尝试使用“this.avatar”,但它不存在。 for 循环中的头像不会传递到您的脚本。你应该这样做:

<v-img
...
@click="selectAvatar(i)"
></v-img>

在你的脚本中:

methods: {
selectAvatar(i){
this.selectedAvatar = this.avatars[i].src
console.log('Avatar selected')
}
}

关于javascript - 使用 Nuxt 和 Vuetify 的头像选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55913719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com