为什么要数据持久化

页面一刷新,刚刚操作的数据就没了
main.js

  1. Vue.prototype.$bus=new Vue();

App.vue

  1. <template>
  2. <div id="app">
  3. <img alt="Vue logo" src="./assets/logo.png" />
  4. <h1>{{ title }}</h1>
  5. <ul>
  6. <li v-for="(item, index) in cartList" :key="item.id">
  7. <h2>{{ item.title }},价格:{{ item.price }}</h2>
  8. <button @click='addCart(index)'>添加购物车</button>
  9. </li>
  10. </ul>
  11. <MyCart :title="title"></MyCart>
  12. <!-- 3.使用 -->
  13. </div>
  14. </template>
  15. <script>
  16. import MyCart from "./components/Cart.vue"; // <!-- 1.导入 -->
  17. export default {
  18. name: "App",
  19. data() {
  20. return {
  21. cartList: [
  22. // { id: 1, title: "Vue实战开发", price: 66, count:2 },
  23. // { id: 2, title: "Django实战开发", price: 88, count:3 },
  24. ],
  25. };
  26. },
  27. methods: {
  28. addCart(i){
  29. const good = this.cartList[i];
  30. this.$bus.$emit('addCart', good);
  31. }
  32. },
  33. async created() {
  34. try {
  35. const res = await this.$http.get("/api/cartList");
  36. this.cartList = res.data.result;
  37. } catch (error) {
  38. console.log(error);
  39. }
  40. },
  41. components: {
  42. MyCart, // <!-- 2.挂载 -->
  43. },
  44. };
  45. </script>
  46. <style>
  47. #app {
  48. }
  49. </style>

Cart.vue

  1. <template>
  2. <div>
  3. <h2>{{ title }}</h2>
  4. <table border="1">
  5. <tr>
  6. <th>#</th>
  7. <th>课程</th>
  8. <th>单价</th>
  9. <th>数量</th>
  10. <th>总价</th>
  11. </tr>
  12. <tr v-for="c in cart" :key="c.id">
  13. <td>
  14. <input type="checkbox" v-model="c.active" />
  15. </td>
  16. <td>{{ c.title }}</td>
  17. <td>{{ c.price }}</td>
  18. <td>
  19. <button>-</button>
  20. {{ c.count }}
  21. <button>+</button>
  22. </td>
  23. <td>{{ c.price * c.count }}</td>
  24. </tr>
  25. </table>
  26. </div>
  27. </template>
  28. <script>
  29. export default {
  30. name: "cart",
  31. props: ["title"],
  32. data() {
  33. return {
  34. cart: [],
  35. };
  36. },
  37. created() {
  38. // $on 绑定事件
  39. this.$bus.$on("addCart", (good) => {
  40. const ret = this.cart.find((v) => v.id === good.id);
  41. console.log(ret);
  42. if (!ret) {
  43. //购物车没有数据
  44. this.cart.push(good);
  45. }else{
  46. ret.count += 1;
  47. }
  48. });
  49. },
  50. };
  51. </script>
  52. <style lang="sass" scoped>
  53. </style>

tmp.gif

如何做数据持久化

image.png
Cart.vue

  1. <template>
  2. <div>
  3. <h2>{{ title }}</h2>
  4. <table border="1">
  5. <tr>
  6. <th>#</th>
  7. <th>课程</th>
  8. <th>单价</th>
  9. <th>数量</th>
  10. <th>总价</th>
  11. </tr>
  12. <tr v-for="c in cart" :key="c.id">
  13. <td>
  14. <input type="checkbox" v-model="c.active" />
  15. </td>
  16. <td>{{ c.title }}</td>
  17. <td>{{ c.price }}</td>
  18. <td>
  19. <button>-</button>
  20. {{ c.count }}
  21. <button>+</button>
  22. </td>
  23. <td>{{ c.price * c.count }}</td>
  24. </tr>
  25. </table>
  26. </div>
  27. </template>
  28. <script>
  29. export default {
  30. name: "cart",
  31. props: ["title"],
  32. data() {
  33. return {
  34. cart: JSON.parse(localStorage.getItem('cart')) || [],
  35. };
  36. },
  37. watch:{
  38. cart: {
  39. handler(n){
  40. this.setLocalData(n);
  41. },
  42. deep: true
  43. }
  44. },
  45. methods:{
  46. setLocalData(n){
  47. localStorage.setItem('cart', JSON.stringify(n))
  48. }
  49. },
  50. created() {
  51. // $on 绑定事件
  52. this.$bus.$on("addCart", (good) => {
  53. const ret = this.cart.find((v) => v.id === good.id);
  54. console.log(ret);
  55. if (!ret) {
  56. //购物车没有数据
  57. this.cart.push(good);
  58. }else{
  59. ret.count += 1;
  60. }
  61. });
  62. },
  63. };
  64. </script>
  65. <style lang="sass" scoped>
  66. </style>

$bus显得有点臃肿,用vuex会更方便。