使用模块在magento 2中的每个产品页面添加脚本标记

使用模块在magento 2中的每个产品页面添加脚本标记

问题描述:

I'm new to magento.

I'm created a module-2 module from below mentioned tutorial.

http://inchoo.net/magento-2/how-to-create-a-basic-module-in-magento-2/

With that module I'm injecting script into the page. But with this I'm limited to execute only on my module page i.e.,

http://localhost/magento2/helloworld

I have a sample store installed, but what should I do if I want my module to be executed on any product page.

Please help me understand. Pointing to an article would be greatly appreciated.

Thank you.

我是magento的新手。 p>

我从下面提到的教程中创建了一个模块2模块。 p>

  http://inchoo.net/magento-2  / how-to-create-a-basic-module-in-magento-2 / 
  code>  pre> 
 
 

使用该模块,我将脚本注入页面。 但有了这个,我只能在我的模块页面上执行,即 p>

  http:// localhost / magento2 / helloworld 
  code>  pre> \  n 
 

我安装了样品库,但如果我希望我的模块在任何产品页面上执行,我该怎么办。 p>

请帮助我理解。 非常感谢指向一篇文章。 p>

谢谢。 p> div>

Call your css and js file inside of tag in the layout xml. Example:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <head>
        <link src="Namespace_YourModule::script.js"/>
        <css src="Namespace_YourModule::style.css"/>
    </head>
    <body>
    </body>
</page>

I found the article that solves this specific problem.

https://mage2.pro/t/topic/34

In this he has added script files by default rather than by route.

edit: In another article he has given information on conditional loading of script and css files.

http://www.webspeaks.in/2016/03/how-to-add-css-and-js-in-magento-2-custom-module.html