vue 如何动态切换全局的placeholder图片

我在assets中存储了各主题对应的placeholder文件,想通过eventbus和全局mixin动态更换placeholder图片,虽然app.vue中的placeHolderNothing改变了,但是img的src并没有值,为什么?

<img :src="imgUrl||placeHolderNothing"/>

App.vue

created () {
      bus.$on('changeTheme', (themeId) => {
        themeId = +themeId
        let theme = 'purple'
        switch (themeId) {
          case 1:
            theme = 'purple'
            break
          case 2:
            theme = 'red'
            break
          case 3:
            theme = 'blue'
            break
          default:
            theme = 'purple'
        }
        this.placeHolderNothing = require(`@/assets/icon_nothing_${theme}.svg`)
      })

      Vue.mixin({
        data () {
          return {
            placeHolderNothing: this.placeHolderNothing
          }
        }
      })
      
      

切换主题时

    bus.$emit('changeTheme', this.themeId)
    
    
      
阅读 3.1k
1 个回答

检查一下imgUrl是''还是' '

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题