我想在模块中添加JS和CSS文件到后台。但是我收到错误消息:试图调用类“ AdminModulesController”的未定义方法“ registerStylesheet”。
我看过其他帖子(例如Show my module JS at footer in prestashop)或此处https://devdocs.prestashop.com/1.7/themes/getting-started/asset-management/
因此,我想避免使用addJS()函数,因为它已被贬值。但是,当我尝试使用$ this-> context-> controller-> registerStylesheet()和$ this-> context-> controller-> registerJavascript()时,出现上述错误。
这是我的整个钩子代码:
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()方法。我想念什么?我完全按照互联网上任何地方的描述进行操作,为什么会出现错误?
最佳答案
使用哪种方法的说明:
这些是PrestaShop 1.7中的FrontController
方法:registerJavascript
和registerStylesheet
。
这些是PrestaShop 1.7中的旧式(不推荐使用)FrontController
方法:addJS
和addCSS
。
这些是PrestaShop 1.7、1.6、1.5中的AdminController
方法:addJS
和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');
}
有关其他信息,请参见我的另一个答案how to register JavaScript in a back-office (in admin pages)。这个问题之后,我已经对其进行了更新。
关于javascript - 无法将js和CSS文件添加到后台,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58605262/