自定义指令处理头像加载失败

This commit is contained in:
jiutianzhiyu 2021-03-25 19:47:32 +08:00
parent e1ac52be71
commit afb4daf19d
3 changed files with 56 additions and 49 deletions

11
src/directives/index.js Normal file
View 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
}
}
})

View File

@ -11,7 +11,7 @@
<div class="right-menu"> <div class="right-menu">
<el-dropdown class="avatar-container" trigger="click"> <el-dropdown class="avatar-container" trigger="click">
<div class="avatar-wrapper"> <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> <span class="name">{{ $store.getters.name }}</span>
<i class="el-icon-caret-bottom" style="color:#fff" /> <i class="el-icon-caret-bottom" style="color:#fff" />
</div> </div>
@ -54,11 +54,6 @@ export default {
async logout() { async logout() {
await this.$store.dispatch('user/logout') await this.$store.dispatch('user/logout')
this.$router.push(`/login?redirect=${this.$route.fullPath}`) 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'
} }
} }
} }

View File

@ -1,43 +1,44 @@
import Vue from 'vue' import Vue from 'vue'
import '@/directives'
import 'normalize.css/normalize.css' // A modern alternative to CSS resets
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 ElementUI from 'element-ui'
import locale from 'element-ui/lib/locale/lang/en' // lang i18n 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 '@/styles/index.scss' // global css
import App from './App'
import store from './store' import App from './App'
import router from './router' import store from './store'
import router from './router'
import '@/icons' // icon
import '@/permission' // permission control import '@/icons' // icon
import '@/permission' // permission control
/**
* If you don't want to use mock-server /**
* you want to use MockJs for mock api * If you don't want to use mock-server
* you can execute: mockXHR() * 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 ! ! ! * 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') if (process.env.NODE_ENV === 'production') {
mockXHR() const { mockXHR } = require('../mock')
} mockXHR()
}
// set ElementUI lang to EN
Vue.use(ElementUI, { locale }) // set ElementUI lang to EN
// 如果想要中文版 element-ui按如下方式声明 Vue.use(ElementUI, { locale })
// Vue.use(ElementUI) // 如果想要中文版 element-ui按如下方式声明
// Vue.use(ElementUI)
Vue.config.productionTip = false
Vue.config.productionTip = false
new Vue({
el: '#app', new Vue({
router, el: '#app',
store, router,
render: h => h(App) store,
}) render: h => h(App)
})