首页 > 其他分享 >Webpack 系列第 3 部分

Webpack 系列第 3 部分

时间:2024-09-21 16:24:57浏览次数:1  
标签:product gt 系列 productlist true cart react Webpack 部分

请查找该系列的旧部分以充分理解这个概念。webpack 5 系列第 1 部分webpack 5 系列第 2 部分webpack 5 系列第 4 部分 在线电子商店申请让我们使用微前端来构建一个在线商店应用程序以实现模块化。每个微前端将代表商店的不同部分,它们将共享公共库,例如 react、设计系统和共享实用程序库。目标:productlist 公开可以由其他应用导入和使用的产品列表。购物车公开了从购物车添加/删除产品的功能。结帐 使用购物车中的数据并处理结帐。 模块联合的配置微前端1:产品列表公开 productlist 组件以供其他微前端使用。// webpack.config.js (productlist)const { modulefederationplugin } = require('webpack').container;module.exports = { plugins: [ new modulefederationplugin({ name: 'productlistapp', filename: 'remoteentry.js', exposes: { './productlist': './src/productlist', }, shared: { react: { singleton: true, eager: true }, 'react-dom': { singleton: true, eager: true }, // share any other libraries like a ui library, e.g., material-ui }, }), ],};登录后复制微前端 2:购物车公开 cart 组件,它使用共享状态库(如 zustand)进行购物车管理。// webpack.config.js (cart)const { modulefederationplugin } = require('webpack').container;module.exports = { plugins: [ new modulefederationplugin({ name: 'cartapp', filename: 'remoteentry.js', exposes: { './cart': './src/cart', }, shared: { react: { singleton: true, eager: true }, 'react-dom': { singleton: true, eager: true }, zustand: { singleton: true }, // zustand or redux for shared state }, }), ],};登录后复制微前端3:结账使用 cart 和 productlist 组件以在结帐前显示摘要。// webpack.config.js (checkout)const { modulefederationplugin } = require('webpack').container;module.exports = { plugins: [ new modulefederationplugin({ name: 'checkoutapp', remotes: { productlistapp: 'productlistapp@http://localhost:3001/remoteentry.js', cartapp: 'cartapp@http://localhost:3002/remoteentry.js', }, shared: { react: { singleton: true, eager: true }, 'react-dom': { singleton: true, eager: true }, }, }), ],};登录后复制 组件实现:微前端1:产品列表由 productlist 微前端公开。// src/productlist.js (productlist)import react from 'react';const products = [ { id: 1, name: 'product 1', price: 50 }, { id: 2, name: 'product 2', price: 75 },];const productlist = () =&gt; ( <div> <h2>products</h2> <ul> {products.map(product =&gt; ( <li key="{product.id}"> {product.name} - ${product.price} </li> ))} </ul></div>);export default productlist;登录后复制微前端 2:购物车由 cart 微前端公开并管理状态(例如,使用 zustand 或 redux)。// src/cart.js (cart)import react from 'react';import create from 'zustand';// zustand store for managing the cartconst usecartstore = create(set =&gt; ({ cart: [], addtocart: (product) =&gt; set(state =&gt; ({ cart: [...state.cart, product] })), removefromcart: (product) =&gt; set(state =&gt; ({ cart: state.cart.filter(item =&gt; item.id !== product.id) })),}));const cart = () =&gt; { const { cart, addtocart, removefromcart } = usecartstore(); return ( <div> <h2>cart</h2> <ul> {cart.map(product =&gt; ( <li key="{product.id}"> {product.name} - ${product.price} <button onclick="{()"> removefromcart(product)}&gt;remove</button> </li> ))} </ul></div> );};export default cart;登录后复制微前端3:结账使用 cart 和 productlist 组件,将所有内容整合在一起。// src/Checkout.js (Checkout)import React, { lazy, Suspense } from 'react';const ProductList = lazy(() =&gt; import('productListApp/ProductList'));const Cart = lazy(() =&gt; import('cartApp/Cart'));const Checkout = () =&gt; ( <div> <h1>Checkout</h1> <suspense fallback="{&lt;div">Loading Products...</suspense></div>}&gt; <productlist></productlist><suspense fallback="{&lt;div">Loading Cart...}&gt; <cart></cart></suspense><button>Proceed to Payment</button> );export default Checkout;登录后复制 运行应用程序的步骤:运行微前端:每个微前端(productlist、cart、checkout)将在不同的端口上提供服务(例如,productlist 在 localhost:3001 上,cart 在 localhost:3002 上,checkout 在 localhost:3003 上)。您需要使用 webpack 开发服务器设置每个微前端并单独运行它们。使用远程模块:在 checkout 微前端中,我们从各自的远程微前端动态导入 productlist 和 cart 组件。共享依赖项:每个微前端共享 react、react-dom,以及可能的其他共享依赖项,例如状态库(例如 zustand)或设计系统(material-ui)。 以上就是Webpack 系列第 3 部分的详细内容,更多请关注我的其它相关文章!

标签:product,gt,系列,productlist,true,cart,react,Webpack,部分
From: https://www.cnblogs.com/aow054/p/18424152

相关文章

  • 为什么现在部分的男人都不想结婚了?
    第一章:经济压力与婚姻意愿第一章:经济压力与婚姻意愿近年来,婚姻意愿的下降已成为一个备受关注的话题,对此,我们进行了一系列量化数据分析,以探讨收入水平如何影响男性的婚姻意愿。量化数据分析根据调查数据显示,收入水平与婚姻意愿之间存在显著关联。具体而言,当代男性往往在......
  • 光电式液位传感器LLM12DH05 国产替代英国SST LLC系列型号
    LLM12DH05光电液位传感器是利用光在两种不同介质界面发生反射折射原理,是一种新型接触式点液位测控装置。可以单点检测,TTL兼容数字电平信号输出,适用于运输及军工等行业生产。⚫应用邻域/Application鱼缸/fishtank 热水器/waterheater 咖啡机/Coffeemachine 小家电/Hous......
  • XeeLee系列《猎户座防线》个人翻译
    “短暂生命辉煌燃烧”号脱离了舰队。我们正在追赶一艘幽灵巡洋舰,已经越来越近了。“辉煌”号的维生穹顶清晰可见,看起来就像泰德船长正坐在她的巨大椅子中,船员和他们的器械们——包括几个向我这样的低级水手——则仅仅是漂浮在宇宙中。微弱的光线来自附近的几个灼热年轻星团,来自......
  • CCF-CSP资格认证题解系列——第1次第1题相反数
    #include<iostream>usingnamespacestd;intcnt;//N个非零且各不相同的整数intmain(){ intn; cin>>n; inta[n]; for(inti=0;i<n;i++){ cin>>a[i]; } for(inti=0;i<n;i++){ for(intj=i+1;j<n;j++){ if(a[i]+a[j]==0){ cnt++; ......
  • Acwing题解系列——841. 字符串哈希
    #include<iostream>usingnamespacestd;constintN=100010;constintP=131;/*题解https://www.acwing.com/solution/content/24738/可以 把字符串变成一个p进制数字(哈希值),实现不同的字符串映射到不同的数字。采用字符的ascii码乘上P的次方来计算哈希值。X1X2X......
  • 帝国cms首页一部分出现乱码;帝国CMS首页乱码解决方案
    帝国CMS首页出现乱码通常是因为字符编码设置不一致所导致。以下是一些可能的解决方案来解决首页乱码的问题:1.检查文件编码文本编辑器设置:确保所有文件都是用相同的编码方式保存的,比如UTF-8或GBK。你可以使用文本编辑器(如Notepad++)打开文件并检查编码设置。文件保存:如果发现编......
  • 0915,SOCKET网络编程部分,三种I/O多路复用模型(select ,poll,epoll)
    目录  nc127.0.0.1port01_socket_client.cc01_socket_server.cc02_select_client.cc02_select_server.cc03_poll_server.cc04_epoll_server.cc01_socket_client.cc#include<stdlib.h>#include<string.h>#include<sys/stat.h>#include<sy......
  • java--章面向对象编程(高级部分)
    类变量和类方法类变量类变量内存布局什么是类变量类变量也叫 静态变量/静态属性,是该类的所有对象共享的变量,任何一个该类的对象去访问它时,取到的都是相同的值,同样任何一个该类的对象去修改它时,修改的也是同一个变量。如何定义类变量定义语法:访问修饰符static......
  • Living-Dream 系列笔记 第79期
    P1775典。code#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;constintN=3e2+5;intn,a[N],sum[N],dp[N][N];signedmain(){ ios::sync_with_stdio(0); cin.tie(0); cin>>n; memset(dp,0x3f,sizeofdp); for(inti=1;i<=n;......
  • 【Webpack】三种模式详解
    文章目录一、Webpack模式概述1.模式的作用2.配置模式二、开发模式(development)1.开发模式的特点开发模式的主要特点包括:2.开发模式的配置3.开发模式的实际应用三、生产模式(production)1.生产模式的特点生产模式的主要特点包括:2.生产模式的配置3.生产模式的实......