Condition in v-bind:Style - VueJS

后端 未结 8 2252
遥遥无期
遥遥无期 2020-12-23 19:28

I have an easy question for which I hope you could help:

8条回答
  •  盖世英雄少女心
    2020-12-23 20:11

    Use a computed property for this:

    ... data () { return { item: { featured_photo: null } } }, computed: { background () { return !item.featured_photo ? '#fff' : `url(${item.featured_photo}) center no-repeat` } }, methods: { setPhoto(val) { this.item.featured_photo = val } }

    Edit:

    I'm making a bunch of assumptions here with your API and routes. This is a rough stab at generalizing it:

    ... methods: { async getBackground (type, index) { let r = await axios.get(`/images/${type}/${index}`).then(r => r.data.background) return r || '#fff' } }

提交回复
热议问题