首页 文章

如何在Magento2中使用自定义模块为所有页面添加CSS和JS?

提问于
浏览
2

我正在尝试使用Magento 2中的自定义模块 Magento_Test 为所有网站页面添加自定义CSS和JS文件 . 我在前端使用布局文件添加了CSS和JS文件 .

Magento/Test/view/frontend/layout/default.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <css src="Magento_Test/css/style.css"/>
        <link src="Magento_Test/js/script.js"/>
    </head>
</page>

CSS和JS文件仅包含在自定义模块页面中 . 那么,如何在Magento2中使用自定义模块为所有模板/网站页面添加CSS和JS?

1 回答

  • -1

    根据docs你想要使用 default_head_blocks.xml 而不是 default.xml

    如果您正在使用模块并且已创建这些文件,这对我有用 - Namespace/Module/view/frontend/view/frontend/web/my-styles.css Namespace/Module/view/frontend/view/frontend/web/hello.js

    现在进来

    Namespace/Module/view/frontend/view/layout/default_head_blocks.xml

    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <css src="Namespace_Module::my-styles.css" />
        <link src="Namespace_Module::hello.js" />
    </head>
    

相关问题