Yii2:如何通过composer添加JavaScript库?

rob*_*sch 6 javascript npm composer-php yii2

我根据本指南设置了一个基本应用程序(安装Yii).这没问题.根据指南,我还将fxp/composer-asset-plugin全局添加到composer.phar.也没问题.

现在我已经要求使用q.js作为npm包托管*.但我不知道如何通过作曲家添加它.我知道我可以使用CDN代替,也可以手动下载和存储.但我更喜欢使用作曲家.那么我需要做些什么来完成这项工作呢?

我把它添加到我的composer.json:

"require": {
    "php": ">=5.4.0",
    "yiisoft/yii2": ">=2.0.4",
    "yiisoft/yii2-bootstrap": "*",
    "yiisoft/yii2-swiftmailer": "*",
    "npm-asset/q": "~1.4"               <------
},
Run Code Online (Sandbox Code Playgroud)

并且被叫composer.phar update但是得到了例外:

Your requirements could not be resolved to an installable set of packages.

    Problem 1
    - The requested package npm-asset/q could not be found in any version, 
      there may be a typo in the package name.
Run Code Online (Sandbox Code Playgroud)

这是一种错误的做法吗?

我想知道如何将JS库添加到项目中.我只知道我必须稍后将它添加到资产中,但是目前我无法获得JS文件.

*托管正确的术语?

Arp*_*wal 0

最好的过程是在 asset/AppAsset.php 文件中进行一些更改。

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $js = [
        'js\your_file_name.js',
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}
Run Code Online (Sandbox Code Playgroud)

并在您的 Web 目录中创建 js 文件夹,并在其中放置您的 js 文件。

要包含 js 文件,您需要:

$this->registerJsFile(Yii::$app->request->baseUrl.'/js/your_file_name.js');
Run Code Online (Sandbox Code Playgroud)