本篇文章更新与2019年4月22日,magento 2开发之js篇
希望读者,读了这篇文章教材,能够对magento 2的前段有所了解和掌握,magento2的开发过程还是比较麻烦和复杂的。我们常常碰到自定义 页面template文件,和自定义js文件,
今天我将给喜欢magento的开发者讲述magento 2的widget部件的继承。
第一步:创建requirejs-config.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ var config = { map: { '*': { compareList: 'Magento_Catalog/js/list', relatedProducts: 'Magento_Catalog/js/related-products', upsellProducts: 'Magento_Catalog/js/upsell-products', productListToolbarForm: 'Magento_Catalog/js/product/list/toolbar', catalogGallery: 'Magento_Catalog/js/gallery', priceBox: 'Magento_Catalog/js/price-box', priceOptionDate: 'Magento_Catalog/js/price-option-date', priceOptionFile: 'Magento_Catalog/js/price-option-file', priceOptions: 'Magento_Catalog/js/price-options', priceUtils: 'Magento_Catalog/js/price-utils', catalogAddToCart: 'Magento_Catalog/js/catalog-add-to-cart' } }, config: { mixins: { 'Magento_Theme/js/view/breadcrumbs': { 'Magento_Catalog/js/product/breadcrumbs': true } } } }; |
注意:通过Magento_Catalog/js/product/breadcrumbs.js 继承Magento_Theme/js/view/breadcrumbs.js必须使用mixins
第二步:我们创建breadcrumbs.phtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ /** @var \Magento\Theme\Block\Html\Breadcrumbs $block */ /** @var \Magento\Catalog\ViewModel\Product\Breadcrumbs $viewModel */ $viewModel = $block->getData('viewModel'); ?> <div class="breadcrumbs"></div> <script type="text/x-magento-init"> { ".breadcrumbs": <?= $viewModel->getJsonConfigurationHtmlEscaped() ?> } </script> |
其中.breadcrumbs :
表示的是标签类breadcrumbs 的class 值是block的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | <?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Catalog\ViewModel\Product; use Magento\Catalog\Helper\Data; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\App\ObjectManager; use Magento\Framework\DataObject; use Magento\Framework\Serialize\Serializer\Json; use Magento\Framework\View\Element\Block\ArgumentInterface; use Magento\Framework\Escaper; /** * Product breadcrumbs view model. */ class Breadcrumbs extends DataObject implements ArgumentInterface { /** * Catalog data. * * @var Data */ private $catalogData; /** * @var ScopeConfigInterface */ private $scopeConfig; /** * @var Escaper */ private $escaper; /** * @param Data $catalogData * @param ScopeConfigInterface $scopeConfig * @param Json|null $json * @param Escaper|null $escaper * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function __construct( Data $catalogData, ScopeConfigInterface $scopeConfig, Json $json = null, Escaper $escaper = null ) { parent::__construct(); $this->catalogData = $catalogData; $this->scopeConfig = $scopeConfig; $this->escaper = $escaper ?: ObjectManager::getInstance()->get(Escaper::class); } /** * Returns category URL suffix. * * @return mixed */ public function getCategoryUrlSuffix() { return $this->scopeConfig->getValue( 'catalog/seo/category_url_suffix', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); } /** * Checks if categories path is used for product URLs. * * @return bool */ public function isCategoryUsedInProductUrl(): bool { return $this->scopeConfig->isSetFlag( 'catalog/seo/product_use_categories', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); } /** * Returns product name. * * @return string */ public function getProductName(): string { return $this->catalogData->getProduct() !== null ? $this->catalogData->getProduct()->getName() : ''; } /** * Returns breadcrumb json with html escaped names * * @return string */ public function getJsonConfigurationHtmlEscaped() : string { return json_encode( [ 'breadcrumbs' => [ 'categoryUrlSuffix' => $this->escaper->escapeHtml($this->getCategoryUrlSuffix()), 'userCategoryPathInUrl' => (int)$this->isCategoryUsedInProductUrl(), 'product' => $this->escaper->escapeHtml($this->getProductName()) ] ], JSON_HEX_TAG ); } } |
通过以上代码我们对js的继承有了了解,如果有不懂地方请留言。