如何动态将产品添加到购物车?(Nuxt 3)

huangapple go评论67阅读模式
英文:

how to add products to cart dynamically ? (Nuxt 3)

问题

以下是翻译好的部分:

  1. "As the question asked, I have 1 list of products, each product has an add cart button."

    • 根据问题所述,我有一个产品列表,每个产品都有一个添加购物车按钮。
  2. "And after clicking add cart, the cart icon on the header will automatically display a div informing the user that they have added a product."

    • 单击添加购物车后,页眉上的购物车图标将自动显示一个 DIV,通知用户已添加了一个产品。
  3. "Thank you everyone for the question and support."

    • 感谢大家的问题和支持。
  4. "this is product detail page with button <button class=&quot;ml-5 btn-wide btn-addcart&quot;>&lt;span&gt;Add to cart&lt;/span&gt;"

    • 这是带有按钮的产品详细页面 <button class="ml-5 btn-wide btn-addcart"><span>Add to cart</span>
  5. "And after selecting more products, my cart has not been updated"

    • 在选择更多产品后,我的购物车没有被更新。
英文:

As the question asked, I have 1 list of products, each product has an add cart button.

And after clicking add cart, the cart icon on the header will automatically display a div informing the user that they have added a product.

enter image description here

Thank you everyone for the question and support.

this is product detail page with button &lt;button class=&quot;ml-5 btn-wide btn-addcart&quot;&gt;&lt;span&gt;Add to cart&lt;/span&gt;

&lt;template&gt;
    &lt;div class=&quot;product-detail py-[40px]&quot;&gt;
        &lt;div class=&quot;mx-auto container&quot;&gt;
            &lt;div class=&quot;head-content&quot;&gt;
                &lt;a-breadcrumb separator=&quot;&#187;&quot;&gt;
                  &lt;a-breadcrumb-item&gt;Trang chủ&lt;/a-breadcrumb-item&gt;
                  &lt;a-breadcrumb-item&gt;&lt;nuxt-link to=&quot;/all-product&quot;&gt;RAM ECC&lt;/nuxt-link&gt;&lt;/a-breadcrumb-item&gt;
                  &lt;a-breadcrumb-item href=&quot;&quot;&gt;RAM Samsung DDR4 ECC Registered 64GB 3200AA&lt;/a-breadcrumb-item&gt;
                &lt;/a-breadcrumb&gt;
            &lt;/div&gt;
            &lt;div class=&quot;mt-10 flex justify-start items-start &quot;&gt;
                &lt;div class=&quot;border-2 w-[381px] relative &quot;&gt;
                    &lt;img :src=&quot;ramImg1&quot; alt=&quot;&quot; srcset=&quot;&quot; class=&quot;top-0 overflow-hidden z-10 left-0&quot;&gt;
                &lt;/div&gt;
                &lt;div class=&quot;px-8 flex flex-col&quot;&gt;
                    &lt;div class=&quot;flex flex-col pb-3 border-b-2 mb-3 w-full&quot;&gt;
                        &lt;span class=&quot;text-[15px] text-primary font-semibold&quot;&gt;&lt;a href=&quot;#&quot;&gt;RAM ECC&lt;/a&gt;&lt;/span&gt;
                        &lt;span class=&quot; text-2xl&quot;&gt;RAM Samsung DDR4 ECC Registered 64GB 3200AA&lt;/span&gt;
                    &lt;/div&gt;
                    &lt;span&gt;M&#227; SP: RA021&lt;/span&gt;
                    &lt;span class=&quot;text-[#c00] text-2xl font-medium my-3&quot;&gt;3,900,000 đ&lt;/span&gt;
                    &lt;span class=&quot; text-base text-[#777] mb-5&quot;&gt;
                        – T&#236;nh trạng: mới 100%, date 2022.&lt;br/&gt;
                        – Gi&#225; tr&#234;n kh&#244;ng bao gồm VAT.
                    &lt;/span&gt;
                    &lt;span class=&quot;text-[#f00] text-xl font-medium mb-5&quot;&gt;C&#242;n 8 sản phẩm&lt;/span&gt;
                    &lt;span class=&quot;mb-5 uppercase text-base font-semibold text-[#114eab]&quot;&gt;Bảo h&#224;nh: 3 năm&lt;/span&gt;
                    &lt;div class=&quot;flex gap-3 items-center mb-5&quot;&gt;
                      &lt;button @click=&quot;decrement&quot; class=&quot;bg-[#E6E6E6] w-[42px] h-[42px] rounded-full&quot;&gt;-&lt;/button&gt;
                      &lt;div class=&quot;text-center pt-2 bg-[#E6E6E6] w-[42px] h-[42px] &quot;&gt;{{ value }}&lt;/div&gt;
                      &lt;button @click=&quot;increment&quot; class=&quot;bg-[#E6E6E6] w-[42px] h-[42px] rounded-full&quot;&gt;+&lt;/button&gt;
                      &lt;button class=&quot;ml-5 btn-wide btn-addcart&quot;&gt;&lt;span&gt;Th&#234;m v&#224;o giỏ h&#224;ng&lt;/span&gt;&lt;/button&gt;
                      &lt;nuxt-link to=&quot;/thanh-toan&quot;&gt;&lt;button class=&quot;btn-wide btn-primary&quot; &gt;&lt;span&gt;Mua ngay&lt;/span&gt;&lt;/button&gt;&lt;/nuxt-link&gt;
                    &lt;/div&gt;
                    &lt;div class=&quot;flex gap-5&quot;&gt;
                        &lt;span class=&quot;text-[#524e6f] font-medium&quot;&gt;Chia sẻ tr&#234;n:&lt;/span&gt;
                        &lt;a href=&quot;#&quot; class=&quot;text-[#524e6f]&quot;&gt;&lt;font-awesome-icon icon=&quot;fa-brands fa-facebook-f&quot; style=&quot;height: 18px;&quot;/&gt;&lt;/a&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&quot;my-5 flex flex-col&quot;&gt;
                &lt;span class=&quot;text-primary text-lg font-bold&quot;&gt;TH&#212;NG TIN SẢN PHẨM&lt;/span&gt;
                &lt;span class=&quot; text-base text-[#777] mt-5 leading-10&quot;&gt;
                    – RAM Samsung DDR4 ECC Registered 64GB 3200AA.&lt;br/&gt;
                    – RAM được sản xuất bởi h&#227;ng Samsung, mới 100% 1 box 25 c&#226;y.
                &lt;/span&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/template&gt;

And after selecting more products, my cart has not been updated

&lt;template&gt;
    &lt;div class=&quot;mini-cart &quot;&gt; 
        &lt;a-dropdown&gt;
          &lt;a class=&quot;ant-dropdown-link&quot; @click.prevent&gt;
            &lt;a-badge count=&quot;5&quot;&gt;
                &lt;div class=&quot;bg-white h-[38px] w-[38px] rounded-full&quot;&gt;
                    &lt;font-awesome-icon icon=&quot;fa-solid fa-cart-shopping&quot; style=&quot;color: #0098d9; padding-top: 30%; padding-left: 30%;&quot; /&gt;
                &lt;/div&gt;
            &lt;/a-badge&gt;
          &lt;/a&gt;
          &lt;template #overlay&gt;
                &lt;div class=&quot;w-[300px] bg-white shadow-lg p-5 flex flex-col ml-[5%] mt-[6%]&quot;&gt;
                    &lt;div class=&quot;flex justify-start gap-2&quot;&gt;
                        &lt;div class=&quot;h-[60px] w-[60px]&quot;&gt;
                            &lt;img :src=&quot;ramImg1&quot; alt=&quot;&quot; srcset=&quot;&quot;&gt;
                        &lt;/div&gt;
                        &lt;div class=&quot;flex flex-col text-center mb-3 w-[155px]&quot;&gt;
                            &lt;span class=&quot; text-primary font-bold&quot;&gt;RAM Samsung DDR4 ECC Registered 32GB 3200AA&lt;/span&gt;
                            &lt;span class=&quot;text-[#414042] text-base&quot;&gt;1 x &lt;span class=&quot;text-[#c00] text-base font-bold&quot;&gt;2,200,000 đ&lt;/span&gt;&lt;/span&gt;
                        &lt;/div&gt;
                        &lt;div&gt;
                            &lt;button href=&quot;#&quot; class=&quot;close&quot;&gt;&lt;/button&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                    &lt;center&gt;&lt;button class=&quot;uppercase bg-[#0d6efd1a] py-1 px-[19px] text-primary font-medium rounded-[5px] h-[35px] w-[200px]&quot;&gt;&lt;span&gt;xo&#225; tất cả sản phẩm&lt;/span&gt;&lt;/button&gt;&lt;/center&gt;
                    &lt;div class=&quot;flex justify-between text-base text-[#777] font-bold my-3 border-t-[1px] pt-3&quot;&gt;
                        &lt;span&gt;Tổng số phụ:&lt;/span&gt;
                        &lt;span&gt;2,200,000 đ&lt;/span&gt;
                    &lt;/div&gt;
                    &lt;nuxt-link to=&quot;/gio-hang&quot;&gt;&lt;button class=&quot;w-full mt-[17px] h-10 border-primary border-[1px] text-primary py-1 rounded-full&quot;&gt;&lt;span&gt;Xem giỏ h&#224;ng&lt;/span&gt;&lt;/button&gt;&lt;/nuxt-link&gt;
                    &lt;nuxt-link to=&quot;/thanh-toan&quot; class=&quot;text-white font-medium&quot;&gt;&lt;button class=&quot;w-full mt-[10px] h-10 bg-primary rounded-full py-1 text-white&quot;&gt;&lt;span&gt;Thanh to&#225;n&lt;/span&gt;&lt;/button&gt;&lt;/nuxt-link&gt;
                &lt;/div&gt;
          &lt;/template&gt;
        &lt;/a-dropdown&gt;
    &lt;/div&gt;
&lt;/template&gt;

答案1

得分: 1

考虑阅读有关状态管理的Nuxt文档。您可以使用useState来实现此功能。

英文:

Consider reading the nuxt documentation about state management. You can use useState for this.

huangapple
  • 本文由 发表于 2023年4月17日 15:41:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定