Magento2开发教程NO19-CSS JS添加移除和加载静态资源顺序

Magento 2的JavaScript, CSS 等静态资源是在页面配置文件的 <head>配置。 Magento 2商店页面的 <head> 是在app/code/Magento/Theme/view/frontend/layout/default_head_blocks.xml里设置。 推荐的添加或者移除CSS和JavaScript的方法就是在自定义模板中扩展该文件。下面是参考:

<theme_dir>/Magento_Theme/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>
        <!-- Add local resources -->
    	<css src="css/my-styles.css"/>
    
        <!-- The following two ways to add local JavaScript files are equal -->
        <script src="Magento_Catalog::js/sample1.js"/>
        <link src="js/sample.js"/>
		
    	<!-- Add external resources -->
	<css src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css" src_type="url" />
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js" src_type="url" />
        <link rel="stylesheet" type="text/css" src="http://fonts.proxy.ustclug.org/css?family=Montserrat" src_type="url" /> 
    </head>
</page>

如果是添加外部的资源, src_type="url" 这个属性是必须设置的。

如果是添加Google webfont,必须设置 rel="stylesheet" type="text/css" ,否则不起作用。

可以通过 <link src="js/sample.js"/> or <script src="js/sample.js"/> 两种方法添加JavaScript文件到模板中。

The path to assets is specified relatively to one of the following locations:

  • <theme_dir>/web
  • <theme_dir>/<Namespace>_<Module>/web

Adding conditional comments

Conditional comments are meant to give special instructions for Internet Explorer. In the terms of adding assets, you can add CSS files to be included for a specific version of Internet Explorer. A sample follows:

    <head>
        <css src="css/ie-9.css" ie_condition="IE 9" />
    </head>
</page>

This adds an IE conditional comment in the generated HTML, like in the following example:

<!--[if IE 9]>
<link rel="stylesheet" type="text/css" media="all" href="<your_store_web_address>/pub/static/frontend/OrangeCo/orange/en_US/css/ie-9.css" />
<![endif]-->

In this example, orange is a custom theme created by the OrangeCo vendor.

Magento 2 移除JavaScript, CSS, fonts方法

To remove the static resources, linked in a page <head>, make a change similar to the following in a theme extending file:

app/design/frontend/<Vendor>/<theme>/Magento_Theme/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>
        <!-- Remove local resources -->
        <remove src="css/styles-m.css" />
        <remove src="my-js.js"/>
        <remove src="Magento_Catalog::js/compare.js" />
								
	<!-- Remove external resources -->
        <remove src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css"/>
        <remove src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"/>
        <remove src="http://fonts.proxy.ustclug.org/css?family=Montserrat" /> 
   </head>

Note, that if a static asset is added with a module path (for example Magento_Catalog::js/sample.js) in the initial layout, you need to specify the module path as well when removing the asset.

Magento 2 加载CSS, JavaScript顺序设置

CSS和JS的加载顺序直接决定了你设置的样式是否有效,我们可以通过order属性来设置每个CSS和JS的加载顺序,从而更好的控制Magento的样式输出。

<css src="css/override.css" order="100" />
<css src="css/override2.css" order="100" />
<script src="js/override.js" order="100" />
<script src="js/override2.js" order="100" />

郑重声明:

1 本资源来源于互联网,资源的版权归资源原作者所持有,受《中华人民共和国著作权法》等相关法律保护。

2 由于无法和原作者取得联系,所以上传的部分资源无法先通过原作者的同意就分享给大家了,如本资源侵犯了您(原作者)的权益,请联系我们(微信号 xiaohaimei1989),我们会立马删除您的资源,并向您表达诚挚的歉意!

3 本站是一个公益型网站,分享资源的目的在于传播知识,分享知识,收取一点点打赏的辛苦费是用于网站的日常运营开支,并非用于商业用途。

4 本站资源只提供学习和参考研究使用,使用过后请在第一时间内删除。本站不承担资源被单位或个人商用带来的法律责任。

发表评论