php - How to include UIkit from Bower Asset inside Yii2 AppAsset
问题描述
I have installed a package that enables me add and make use of uikit css and js files.
It enables me to add the css files and js files by adding the following code to main.php
.
\worstinme\uikit\Asset::register($this);
This code adds the css to the header and the js file to the footer. However, I am not able to control their positions so it conflicts with some other files I added. See below:
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/fonts/style.min.css',
'css/fonts/s9dicons.css',
'css/template.css',
];
public $js = [
'js/main.js',
];
public $depends = [
'yii\web\YiiAsset', \\ loads jquery
//'yii\bootstrap\BootstrapAsset',
];
}
In my main layout head, I have the codes below:
\worstinme\uikit\Asset::register($this);
AppAsset::register($this);
If I use the above, it loads uikit.css
first before style.min.css, s9dicons.css and template.css which is fine BUT in the footer, it also loads uikit.js
first before jquery.js
and main.js
which is wrong. If I placed \worstinme\uikit\Asset::register($this);
after AppAsset::register($this);
, the problem will be the other way around.
How can I place this in a way that the uikit.css
file loads first and the uikit.js
file load after jquery.js
but not before main.js
?
The worstinme
application fetches the uikit css and js files from vendor/bower-assets/uikit
. I checked its Asset.php
file and found the code below:
namespace worstinme\uikit;
use yii\web\AssetBundle;
class Asset extends AssetBundle
{
/**
* {@inheritdoc}
*/
public $sourcePath = '@bower/uikit/dist';
/**
* {@inheritdoc}
*/
public $css = [
'css/uikit.min.css',
];
/**
* {@inheritdoc}
*/
public $js = [
'js/uikit.min.js',
];
/**
* {@inheritdoc}
*/
public $depends = [
];
}
So I am thinking, can I avoid using \worstinme\uikit\Asset::register($this);
and just get the uikit.css
and uikit.js
separately from the source into the AppAsset.php
directly as the solution? Seems like I will be able to control their position when separated.
解决方案
UikitAsset.php
我通过在资产文件夹中创建一个文件来解决这个问题。它包含以下代码:
<?php
namespace app\assets;
use yii\web\AssetBundle;
class UikitAsset extends AssetBundle
{
public $sourcePath = '@bower/uikit/dist';
public $baseUrl = '@web';
public $css = [
'css/uikit.min.css',
];
public $js = [
'js/uikit.min.js',
];
}
然后我继续AppAsset.php
并添加如下代码:
<?php
namespace frontend\assets;
use yii\web\AssetBundle;
/**
* Main frontend application asset bundle.
*/
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/fonts/style.min.css',
'css/fonts/s9dicons.css',
'css/template.css',
];
public $js = [
'js/main.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
'\app\assets\UikitAsset',
];
}
这解决了这个问题。将 css 文件和 js 文件放在正确的位置。我也不必再次添加\worstinme\uikit\Asset::register($this);
到我的布局文件头来加载文件。
推荐阅读
- graphql - Apollo 缓存本地过滤器和分页
- javascript - 反应路由器dom v5默认路由不起作用
- php - CURL 警告:curl_file_create() 期望参数 1 是有效路径,给定字符串
- spring-boot - 使用 springboot 2.3.3 和 axon 4.4.2 实现轴突快照
- python - 无法访问我在类内的嵌套函数
- mongodb - 为什么评论即使成功保存在数据库中也没有显示?
- c - 找到连接灯泡代码的最少电线数量
- arrays - 如何在Scala中手动对数组进行排序?
- javascript - 如何存储 jwt 令牌以便重定向到其他子域不需要凭据
- python - 将大小为 2 的 numpy 数组转换为 3