博客
关于我
总结一下vue添加收藏取消收藏功能
阅读量:413 次
发布时间:2019-03-05

本文共 362 字,大约阅读时间需要 1 分钟。

在最近的商城项目中,实现商品收藏与取消收藏功能是一个重要的功能模块。以下是具体的实现方法和思考过程:

进入商品详情页之前,需要传递商品的id和状态信息。id用于唯一标识商品,状态信息用于控制收藏的状态。在页面加载时,通过生命周期钩子created,获取这些信息并初始化UI状态。

具体实现步骤如下:

  • 在收藏按钮点击时,执行以下操作:

    • 将商品数据插入收藏列表(collection.js),以便在收藏页面展示。
    • 更新商品数据列表,传递id和状态信息,确保状态在前后刷新时保持一致。
  • 在生命周期钩子beforeDestroy中,确保状态更新回传至商品列表,避免页面关闭后数据丢失。

  • 在收藏按钮的视觉状态中,根据状态信息进行切换(例如使用已收藏和未收藏的不同图标)。

  • 通过上述方法,可以确保收藏状态在页面刷新或切换时保持正确无误。

    转载地址:http://zcgwz.baihongyu.com/

    你可能感兴趣的文章
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install CERT_HAS_EXPIRED解决方法
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>