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

查看:143
本文介绍了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.

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

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 ,将需要一个已发布的资产.

在这里,您特别有两个选择,第一个可能更快捷,更连贯:

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天全站免登陆