Angular实践中的Tips

2021-08-09 11:48 | (license)

懒加载Libraries

通用做法

const colorThief = () => import('color-thief').then(p => p.default)

依赖注入

// 定义InjectionToken
type LazyColorThief = ReturnType<typeof color-thief>
const COLORTHIEF = new InjectionToken<LazyColorThief>('Lazily loaded colorThief', { provideIn: 'root', factory: color-thief })

// 使用token
constructor(@Inject(COLORTHIEF) private colorThief: LazyColorThief) {
	this.colorThief.then(colorThief => {
        // use it
    })
}