自定义指令处理头像加载失败
This commit is contained in:
		
							parent
							
								
									e1ac52be71
								
							
						
					
					
						commit
						afb4daf19d
					
				
							
								
								
									
										11
									
								
								src/directives/index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/directives/index.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,11 @@
 | 
			
		||||
import Vue from 'vue'
 | 
			
		||||
// 全局指令: 图片加载失败
 | 
			
		||||
Vue.directive('imgerror', {
 | 
			
		||||
  // dom被插入到组件中时, 第二个参数
 | 
			
		||||
  inserted(el, options) {
 | 
			
		||||
    // console.log(el)
 | 
			
		||||
    el.onerror = function() {
 | 
			
		||||
      el.src = options.value
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
})
 | 
			
		||||
@ -11,7 +11,7 @@
 | 
			
		||||
    <div class="right-menu">
 | 
			
		||||
      <el-dropdown class="avatar-container" trigger="click">
 | 
			
		||||
        <div class="avatar-wrapper">
 | 
			
		||||
          <img src="$store.getters.staffPhoto" class="user-avatar" @error="photoErrorHandle">
 | 
			
		||||
          <img v-imgerror="require('@/assets/common/bigUserHeader.png')" src="$store.getters.staffPhoto" class="user-avatar">
 | 
			
		||||
          <span class="name">{{ $store.getters.name }}</span>
 | 
			
		||||
          <i class="el-icon-caret-bottom" style="color:#fff" />
 | 
			
		||||
        </div>
 | 
			
		||||
@ -54,11 +54,6 @@ export default {
 | 
			
		||||
    async logout() {
 | 
			
		||||
      await this.$store.dispatch('user/logout')
 | 
			
		||||
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
 | 
			
		||||
    },
 | 
			
		||||
    // 头像加载失败处理
 | 
			
		||||
    photoErrorHandle(e) {
 | 
			
		||||
      // console.log(e.target)
 | 
			
		||||
      e.target.src = 'https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1851283359,3457678391&fm=26&gp=0.jpg'
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										87
									
								
								src/main.js
									
									
									
									
									
								
							
							
						
						
									
										87
									
								
								src/main.js
									
									
									
									
									
								
							@ -1,43 +1,44 @@
 | 
			
		||||
import Vue from 'vue'
 | 
			
		||||
 | 
			
		||||
import 'normalize.css/normalize.css' // A modern alternative to CSS resets
 | 
			
		||||
 | 
			
		||||
import ElementUI from 'element-ui'
 | 
			
		||||
import 'element-ui/lib/theme-chalk/index.css'
 | 
			
		||||
import locale from 'element-ui/lib/locale/lang/en' // lang i18n
 | 
			
		||||
 | 
			
		||||
import '@/styles/index.scss' // global css
 | 
			
		||||
 | 
			
		||||
import App from './App'
 | 
			
		||||
import store from './store'
 | 
			
		||||
import router from './router'
 | 
			
		||||
 | 
			
		||||
import '@/icons' // icon
 | 
			
		||||
import '@/permission' // permission control
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * If you don't want to use mock-server
 | 
			
		||||
 * you want to use MockJs for mock api
 | 
			
		||||
 * you can execute: mockXHR()
 | 
			
		||||
 *
 | 
			
		||||
 * Currently MockJs will be used in the production environment,
 | 
			
		||||
 * please remove it before going online ! ! !
 | 
			
		||||
 */
 | 
			
		||||
if (process.env.NODE_ENV === 'production') {
 | 
			
		||||
  const { mockXHR } = require('../mock')
 | 
			
		||||
  mockXHR()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// set ElementUI lang to EN
 | 
			
		||||
Vue.use(ElementUI, { locale })
 | 
			
		||||
// 如果想要中文版 element-ui,按如下方式声明
 | 
			
		||||
// Vue.use(ElementUI)
 | 
			
		||||
 | 
			
		||||
Vue.config.productionTip = false
 | 
			
		||||
 | 
			
		||||
new Vue({
 | 
			
		||||
  el: '#app',
 | 
			
		||||
  router,
 | 
			
		||||
  store,
 | 
			
		||||
  render: h => h(App)
 | 
			
		||||
})
 | 
			
		||||
import Vue from 'vue'
 | 
			
		||||
import '@/directives'
 | 
			
		||||
 | 
			
		||||
import 'normalize.css/normalize.css' // A modern alternative to CSS resets
 | 
			
		||||
 | 
			
		||||
import ElementUI from 'element-ui'
 | 
			
		||||
import 'element-ui/lib/theme-chalk/index.css'
 | 
			
		||||
import locale from 'element-ui/lib/locale/lang/en' // lang i18n
 | 
			
		||||
 | 
			
		||||
import '@/styles/index.scss' // global css
 | 
			
		||||
 | 
			
		||||
import App from './App'
 | 
			
		||||
import store from './store'
 | 
			
		||||
import router from './router'
 | 
			
		||||
 | 
			
		||||
import '@/icons' // icon
 | 
			
		||||
import '@/permission' // permission control
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * If you don't want to use mock-server
 | 
			
		||||
 * you want to use MockJs for mock api
 | 
			
		||||
 * you can execute: mockXHR()
 | 
			
		||||
 *
 | 
			
		||||
 * Currently MockJs will be used in the production environment,
 | 
			
		||||
 * please remove it before going online ! ! !
 | 
			
		||||
 */
 | 
			
		||||
if (process.env.NODE_ENV === 'production') {
 | 
			
		||||
  const { mockXHR } = require('../mock')
 | 
			
		||||
  mockXHR()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// set ElementUI lang to EN
 | 
			
		||||
Vue.use(ElementUI, { locale })
 | 
			
		||||
// 如果想要中文版 element-ui,按如下方式声明
 | 
			
		||||
// Vue.use(ElementUI)
 | 
			
		||||
 | 
			
		||||
Vue.config.productionTip = false
 | 
			
		||||
 | 
			
		||||
new Vue({
 | 
			
		||||
  el: '#app',
 | 
			
		||||
  router,
 | 
			
		||||
  store,
 | 
			
		||||
  render: h => h(App)
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user