The specific operations are as follows:
(Learning video sharing:Programming video)
1. Define in assets/AppAsset Method
* @since 2.0 */ class AppAsset extends AssetBundle { public $basePath = '@webroot'; public $baseUrl = '@web'; // 全局 public $css = [ ]; // 全局 public $js = [ ]; public $depends = [ // 'yii\web\YiiAsset', // 'yii\bootstrap\BootstrapAsset', // 注释掉禁用bootstrap ]; // 这是设置所有js放置的位置 public $jsOptions = [ 'position' => \yii\web\View::POS_HEAD, ]; //定义按需加载JS方法 public static function addJs($view, $jsfile) { $view->registerJsFile( $jsfile, [ AppAsset::className(), "depends" => "app\assets\AppAsset" ] ); } //定义按需加载css方法 public static function addCss($view, $cssfile) { $view->registerCssFile( $cssfile, [ AppAsset::className(), "depends" => "app\assets\AppAsset" ] ); } }
2. Call
request->baseUrl."/css/site.css"); // 按需加载js AppAsset::addJs($this, Yii::$app->request->baseUrl."/js/respond.min.js"); ?> beginPage() ?> = Html::csrfMetaTags() ?>= Html::encode($this->title) ?> head() ?> beginBody() ?> = $content ?> endBody() ?> endPage() ?>
in the view. Related recommendations:yii framework
The above is the detailed content of How to load static resources in yii2 framework. For more information, please follow other related articles on the PHP Chinese website!