提交 d7f3e931 编写于 作者: X xjh22222228

fix(#58): icon load

上级 f3abd894
......@@ -2,7 +2,6 @@
*ngIf="src && !hasError; else noSrc"
[src]="src"
class="icon dark-border-color"
(error)="onImgError($event)"
[ngStyle]="{ width: (size || 35) + 'px', height: (size || 35) + 'px' }"
>
......
import { Component, OnInit, Input } from '@angular/core'
import { isValidImg } from '../../utils'
@Component({
selector: 'app-logo',
......@@ -11,15 +12,15 @@ export class LogoComponent implements OnInit {
@Input() colour: string
@Input() size: number
hasError = false
hasError = true
color = '#1890ff'
constructor() { }
ngOnInit(): void {
}
onImgError = () => {
this.hasError = true
async ngOnInit() {
const isValid = await isValidImg(this.src)
if (isValid) {
this.hasError = false
}
}
}
// Copyright @ 2018-2021 xiejiahe. All rights reserved. MIT license.
// See https://github.com/xjh22222228/nav
import qs from 'qs'
import config from '../../nav.config'
......@@ -355,3 +356,24 @@ export function copyText(el: any, text: string): Promise<boolean> {
});
})
}
export async function isValidImg(url: string): Promise<boolean> {
if (!url) return false
if (url === 'null' || url === 'undefined') return false
return new Promise(resolve => {
const img = document.createElement('img')
img.src = url
img.style.display = 'none'
img.onload = () => {
img.parentNode.removeChild(img)
resolve(true)
}
img.onerror = () => {
img.parentNode.removeChild(img)
resolve(false)
}
document.body.append(img)
})
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册