无法将js和CSS文件添加到后台 [英] Can't add js and css files to back office
问题描述
我想在模块中添加JS和CSS文件到后台.但是我收到错误消息:尝试调用类"AdminModulesController"的名为"registerStylesheet"的未定义方法.
I want to add JS and CSS files to back office in my module. But I get error: Attempted to call an undefined method named "registerStylesheet" of class "AdminModulesController".
我看过其他帖子(例如在prestashop的页脚处显示我的模块JS ),或在此处https://devdocs.prestashop.com/1.7/themes/getting-started/asset-management/
I've seen other posts (like this Show my module JS at footer in prestashop) or here https://devdocs.prestashop.com/1.7/themes/getting-started/asset-management/
因此,我想避免使用addJS()函数,因为此函数已过时.但是,当我尝试使用$ this-> context-> controller-> registerStylesheet()和$ this-> context-> controller-> registerJavascript()时,出现上述错误.
So I want to avoid addJS() function as this is depreciated. But when I try to use $this->context->controller->registerStylesheet() and $this->context->controller->registerJavascript() I get the above error.
这是我的整个钩子代码:
This is my whole hook code:
public function hookActionAdminControllerSetMedia($params)
{
$this->context->controller->registerStylesheet(
'mb_pages_content',
'modules/'.$this->name.'/styles/admin.min.css'
);
$this->context->controller->registerJavascript(
'mb_pages_content',
'modules/'.$this->name.'/js/admin.js'
);
}
我检查过我的东西是什么:$ this-> context-> controller但实际上并没有registerStylesheet()和registerJavascript()方法.我想念什么?我所做的一切都完全按照互联网上任何地方的描述进行操作,为什么我会收到错误消息?
I've checked what kind of thing is my: $this->context->controller but it doesn't indeed have registerStylesheet() and registerJavascript() methods. What am I missing? i do everything exactly as described everywhere in the internet, why do I get the error?
推荐答案
使用哪种方法的说明:
这些是PrestaShop 1.7中的 FrontController
方法: registerJavascript
和 registerStylesheet
.
These are FrontController
methods in PrestaShop 1.7: registerJavascript
and registerStylesheet
.
这些是PrestaShop 1.7中的旧版(不推荐使用)FrontController 方法: addJS
和 addCSS
.
These are legacy (deprecated) FrontController
methods in PrestaShop 1.7: addJS
and addCSS
.
这些是PrestaShop 1.7、1.6、1.5中的 AdminController
方法: addJS
和 addCSS
.
These are AdminController
methods in PrestaShop 1.7, 1.6, 1.5: addJS
and addCSS
.
因此,通过模块类为后台添加JS和CSS文件(例如AdminController)的正确示例是:
public function hookActionAdminControllerSetMedia($params)
{
// Adds your's CSS file from a module's directory
$this->context->controller->addCSS($this->_path . 'views/css/example.css');
// Adds your's JavaScript file from a module's directory
$this->context->controller->addJS($this->_path . 'views/js/example.js');
}
有关其他信息,请参见我的另一个答案如何在后台(在管理页面中)注册JavaScript .这个问题之后,我已经对其进行了更新.
For an additional information see my yet another answer how to register JavaScript in a back-office (in admin pages). I have updated it after this question.
这篇关于无法将js和CSS文件添加到后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!