1

我想在我的模块中将 JS 和 CSS 文件添加到后台。但我收到错误:尝试调用类“AdminModulesController”的名为“registerStylesheet”的未定义方法。

我看过其他帖子(比如在 prestashop 的页脚处显示我的模块 JS)或这里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() 方法。我错过了什么?我所做的一切都完全按照互联网上的描述进行,为什么会出现错误?

4

2 回答 2

3

使用哪些方法的说明:

这些是FrontControllerPrestaShop 1.7 中的方法:registerJavascriptregisterStylesheet.

这些是FrontControllerPrestaShop 1.7 中的遗留(已弃用)方法:addJSaddCSS.

这些是AdminControllerPrestaShop 1.7、1.6、1.5:addJSaddCSS.

因此,通过模块类为后台(即 AdminController)添加 JS 和 CSS 文件的正确示例是:

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。我在这个问题之后更新了它。

于 2019-10-30T04:38:48.537 回答
0

尝试:

$this->addJs(
     _PS_MODULE_DIR_ .'objet/views/js/feature.js',
     'all'
);
$this->addCss(
      _PS_MODULE_DIR_ .'objet/views/css/feature.css',
      'all'
);

问候

于 2019-10-29T10:24:05.663 回答