黑鸟 / 外部资源加载器
通过URL轻松实现脚本和样式的懒加载
1.0.6
2024-07-01 15:41 UTC
Requires
- php: >=7.4.0
- magento/framework: *
README
这个小巧的Magento 2模块将允许您通过URL轻松实现脚本和样式的懒加载。
安装
composer require blackbird/external-resources-loader
php bin/magento setup:upgrade
使用方法
加载脚本
blackbird.loadExternalResource('https://cdn.jsdelivr.net.cn/npm/@splidejs/splide@4.1.4/dist/js/splide.min.js') .then(() => /* code which is executed after the lib has loaded */)
加载样式
blackbird.loadExternalResource('https://cdn.jsdelivr.net.cn/npm/@splidejs/splide@4.1.4/dist/css/splide.min.css') .then(() => /* code which is executed after the style has loaded */)
加载多个相关脚本和样式
Promise.all([ blackbird.loadExternalResource('https://cdn.jsdelivr.net.cn/npm/@splidejs/splide@4.1.4/dist/js/splide.min.js'), blackbird.loadExternalResource('https://cdn.jsdelivr.net.cn/npm/@splidejs/splide@4.1.4/dist/css/splide.min.css') ]).then(() => /* code which is executed after the style and the lib has loaded */)
故障排除
loadExternalResource
方法将根据URL扩展名加载脚本或样式。如果URL不以 .js
或 .css
结尾,您可以使用以下两种方法之一来懒加载资源
loadExternalScript
: 从提供javascript内容的URL加载资源loadExternalStyle
: 从提供css内容的URL加载资源
例如
blackbird.loadExternalScript("<?= $block->getViewFileUrl('js/my-script') ?>")