yii2 注册 JS 文件到视图 [英] yii2 registering JS files to a View

查看:28
本文介绍了yii2 注册 JS 文件到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 /views/A/ 文件夹中有 A.php 视图文件.我在 /views/A/ 文件夹中有 A.js js 文件请帮我在视图文件中注册js文件.

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.

据我所知,我必须写$this->registerJsFile('path/to/file.js'); 在视图文件中.

As I understand I must write $this->registerJsFile('path/to/file.js'); in view file.

但是(问题 A)我从 PHPStorm 收到 method registerJsFile is not found in a class 消息.

But (Question A) I get method registerJsFile is not found in a class message from PHPStorm.

另外(问题 B)考虑到两个文件都在同一个文件夹 /views/A/ 中,我应该在路径中写什么?

Also (Question B) what should I write in path considering both files are in the same folder /views/A/ ?

推荐答案

是否有任何特定原因需要手动包含文件而不是创建资产包?

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, published and external assets.

其中最重要的部分是已发布资产使用不同的选项来确定是否以及如何发布文件.

The most important part of it being that source and published assets use different options to determine whether and how a file should be published.

就您而言,您有一个 source 资产,需要将其复制到资产目录中.

In your case you've got a source asset which needs to be copied over to the assets directory.

registerJsFile 的调用作为 hinted在文档中,将期待已发布资产.

The invocation of registerJsFile as hinted in the documentation, will expect a published asset.

这里有两个选项,第一个可能更快捷、更连贯:

Here you have specifically two options, which probably the first is more quick and coherent:

  1. 移动 web/ 文件夹中的资源,如 web/js/ 或任何你喜欢的,并继续使用 registerJsFile()
  2. 资产添加一个新的资产包,指定上面链接页面中详述的各种选项.
  1. move the asset in the web/ folder, as in web/js/ or whatever you prefer and keep using registerJsFile()
  2. add a new asset bundle for source assets, specifying the various options as detailed in the above linked page.

希望这能解决问题.

这篇关于yii2 注册 JS 文件到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆