Javascript yii2 注册 JS 文件到视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27656777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
yii2 registering JS files to a View
提问by David
I have A.php view file in /views/A/folder.
And I have A.js js file in /views/A/folder
Please help me register js file in view file.
我在/views/A/文件夹中有 A.php 视图文件。我在/views/A/文件夹中有 A.js js 文件请帮我在视图文件中注册 js 文件。
As I understand I must write
$this->registerJsFile('path/to/file.js');in view file.
据我了解,我必须$this->registerJsFile('path/to/file.js');在视图文件中写入
。
But (Question A) I get method registerJsFile is not found in a classmessage from PHPStorm.
但是(问题 A)我收到method registerJsFile is not found in a class来自 PHPStorm 的消息。
Also (Question B) what should I write in path considering both files are in the same folder /views/A/?
另外(问题B)考虑到两个文件都在同一个文件夹中,我应该在路径中写什么/views/A/?
采纳答案by Mr Peach
is there any specific reason to include the file manually rather than creating an asset bundle?
是否有任何特定原因手动包含文件而不是创建资产包?
In any case if you've read the documentation regarding assets, you would have noticed that there's a clear distinction about source, publishedand externalassets.
在任何情况下,如果您阅读了有关 assets的文档,您就会注意到source、published和externalassets 之间存在明显区别。
The most important part of it being that sourceand publishedassets use different options to determine whether and how a file should be published.
其中最重要的部分是源和已发布资产使用不同的选项来确定是否以及如何发布文件。
In your case you've got a sourceasset which needs to be copied over to the assets directory.
在您的情况下,您有一个需要复制到资产目录的源资产。
The invocation of registerJsFileas hinted in the documentation, will expect a publishedasset.
的调用registerJsFile是暗示在文档中,将期待公布的资产。
Here you have specifically two options, which probably the first is more quick and coherent:
在这里你有两个特别的选择,第一个可能更快速和连贯:
- move the asset in the
web/folder, as inweb/js/or whatever you prefer and keep usingregisterJsFile() - add a new asset bundle for sourceassets, specifying the various options as detailed in the above linked page.
- 移动
web/文件夹中的资产,如在web/js/或任何您喜欢的方式并继续使用registerJsFile() - 为源资产添加一个新的资产包,指定上面链接页面中详述的各种选项。
Hope this clears things out.
希望这能解决问题。
回答by xav
This is not elegant, but working if you need to have your js file registered after jquery (as seen in the Yii2 doc)
这并不优雅,但是如果您需要在 jquery 之后注册您的 js 文件(如 Yii2 文档中所示)
<?php $this->registerJsFile(Yii::$app->request->baseUrl.'/js/youFile.js',['depends' => [\yii\web\JqueryAsset::className()]]); ?>
回答by Ilyas karim
If you register a js file by:
如果您通过以下方式注册 js 文件:
$this->registerJsFile("@web/js/all.js");
This will work but you will not be able to use jQuery. Because this file all.jsis loaded before jQuery. To load after jQuery we make it depend it on 'yii\web\YiiAsset'or on \yii\web\JqueryAsset. So it will be loaded after jQuery.js. Example:
这会起作用,但您将无法使用 jQuery。因为这个文件all.js是在 jQuery 之前加载的。要在 jQuery 之后加载,我们让它依赖于'yii\web\YiiAsset'或 on \yii\web\JqueryAsset。所以它会在jQuery.js. 例子:
$this->registerJsFile("@web/js/all.js",[
'depends' => [
\yii\web\JqueryAsset::className()
]
]);
So What is difference between \yii\web\JqueryAssetand \yii\web\YiiAsset?
那么\yii\web\JqueryAsset和之间有什么区别\yii\web\YiiAsset?
In jQueryAssetthe js file will load after jQuery.jsand in YiiAssetthe js file will load after yii.jsfile.
在jQueryAssetjs 文件中将加载之后jQuery.js,在YiiAssetjs 文件中将在yii.js文件之后加载。
If you want to create your own custom Asset Bundle:
如果您想创建自己的自定义资产包:
<?php
namespace frontend\components;
use yii;
use yii\web\AssetBundle;
class CustomAssets extends AssetBundle
{
public $css = [
"path/to/css/file.css"
];
public $js = [
"path/to/js/file.js"
];
public $depends = [
];
}
回答by Vikram Pote
Register your js file on given possion
在给定的位置上注册您的 js 文件
$this->registerJsFile('path/to/file.js', ['position' => \yii\web\View::POS_END]);
The first argument is the actual JS code we want to insert into the page. The second argument determines where script should be inserted into the page. Possible values are:
第一个参数是我们想要插入到页面中的实际 JS 代码。第二个参数确定应将脚本插入页面的何处。可能的值为:
View::POS_HEAD for head section.
View::POS_BEGIN for right after opening .
View::POS_END for right before closing .
View::POS_READY for executing code on document ready event.
View::POS_HEAD 为头部部分。
View::POS_BEGIN 打开后立即显示。
View::POS_END 在关闭之前。
View::POS_READY 用于在文档就绪事件上执行代码。
This will register jQuery automatically. View::POS_LOAD for executing code on document load event. This will register jQuery automatically. The last argument is a unique script ID that is used to identify code block and replace existing one with the same ID instead of adding a new one. If you don't provide it, the JS code itself will be used as the ID.
这将自动注册 jQuery。View::POS_LOAD 用于在文档加载事件上执行代码。这将自动注册 jQuery。最后一个参数是唯一的脚本 ID,用于标识代码块并用相同的 ID 替换现有的代码块,而不是添加新的代码块。如果不提供,则使用 JS 代码本身作为 ID。
An external script can be added like the following:
可以添加外部脚本,如下所示:
$this->registerJsFile('http://example.com/js/main.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
The arguments for registerJsFile() are similar to those for registerCssFile(). In the above example, we register the main.js file with the dependency on JqueryAsset. This means the main.js file will be added AFTER jquery.js. Without this dependency specification, the relative order between main.js and jquery.js would be undefined.
registerJsFile() 的参数类似于 registerCssFile() 的参数。在上面的例子中,我们使用对 JqueryAsset 的依赖来注册 main.js 文件。这意味着 main.js 文件将在 jquery.js 之后添加。如果没有这个依赖规范,main.js 和 jquery.js 之间的相对顺序将是未定义的。
回答by J?rgen
A: From the docs: http://www.yiiframework.com/doc-2.0/yii-web-view.htmlYour code seem correct.
答:来自文档:http: //www.yiiframework.com/doc-2.0/yii-web-view.html您的代码似乎是正确的。
Do you register the js from the view file itself? not the controller? The registerJsFile() method is from the view class.
您是否从视图文件本身注册了 js?不是控制器?registerJsFile() 方法来自视图类。
Its highly possible that your IDE is not finding the method, have you tried it in a apache enviroment?
您的IDE很可能没有找到该方法,您是否在apache环境中尝试过?
B: Use a alias
B:使用别名

