{"id":50363,"date":"2025-11-27T17:22:50","date_gmt":"2025-11-27T09:22:50","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=50363"},"modified":"2025-11-27T17:22:50","modified_gmt":"2025-11-27T09:22:50","slug":"%e7%94%a8vue%e5%81%9aapp%e8%b4%ad%e7%89%a9%e8%bd%a6%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2025\/50363\/","title":{"rendered":"\u7528vue\u505aapp\u8d2d\u7269\u8f66\u600e\u4e48\u5b9e\u73b0\uff1f"},"content":{"rendered":"<p>Vue \u662f\u4e00\u79cd\u5148\u8fdb\u7684 JavaScript \u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u4e3a\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u521b\u5efa\u4ea4\u4e92\u5f0f\u7528\u6237\u754c\u9762\u3002Vue \u8fd8\u5177\u6709\u73b0\u4ee3\u5316\u7684\u5de5\u5177\u96c6\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6784\u5efa\u66f4\u5feb\uff0c\u66f4\u9ad8\u6548\u7684 SPA \u5e94\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Vue \u521b\u5efa\u4e00\u4e2a App \u8d2d\u7269\u8f66\u3002<\/p>\n<p>## \u7b80\u4ecb<\/p>\n<p>\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u8d2d\u7269\u8f66\uff0c\u5b83\u5c06\u5305\u542b\u7528\u6237\u6dfb\u52a0\u5230\u5176\u8d2d\u7269\u8f66\u4e2d\u7684\u5546\u54c1\uff0c\u5e76\u4e14\u8fd8\u5c06\u663e\u793a\u8d2d\u7269\u8f66\u7684\u603b\u8ba1\u3002\u5f53\u7528\u6237\u8d2d\u4e70\u4e86\u4e00\u4e9b\u5546\u54c1\u65f6\uff0c\u6211\u4eec\u5c06\u4ece\u8d2d\u7269\u8f66\u4e2d\u5220\u9664\u76f8\u5e94\u7684\u5546\u54c1\u3002<\/p>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528 Vue \u7684\u4e00\u4e9b\u6838\u5fc3\u6982\u5ff5\uff0c\u5305\u62ec\u7ec4\u4ef6\uff0cprops\uff0c\u4e8b\u4ef6\u548c\u8ba1\u7b97\u5c5e\u6027\u6765\u521b\u5efa\u8fd9\u6837\u7684\u8d2d\u7269\u8f66\u3002<\/p>\n<p>## \u7ec4\u4ef6<\/p>\n<p>\u7ec4\u4ef6\u662f Vue \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u4e00\u9879\u6838\u5fc3\u6982\u5ff5\u3002\u7ec4\u4ef6\u662f\u4e00\u4e2a\u53ef\u91cd\u590d\u4f7f\u7528\u7684\u4ee3\u7801\u5757\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5728 Vue \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>\u5728\u6211\u4eec\u7684\u8d2d\u7269\u8f66\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u4e00\u4e2a\u7531\u4e24\u4e2a\u7ec4\u4ef6\u7ec4\u6210\u7684\u4f53\u7cfb\u7ed3\u6784\uff1a\u4e00<a href=\"https:\/\/www.zhidianwl.com\/\">\u7f51\u7ad9\u505aapp<\/a>\u4e2a\u5546\u54c1\u5217\u8868\u7ec4\u4ef6\u548c\u4e00\u4e2a\u8d2d\u7269\u8f66\u7ec4\u4ef6\u3002<\/p>\n<p>### \u5546\u54c1\u5217\u8868\u7ec4\u4ef6<\/p>\n<p>\u5546\u54c1\u5217\u8868\u7ec4\u4ef6\u5c06\u662f\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u5165\u53e3\u70b9\u3002\u5b83\u4f1a\u5c06\u7528\u6237\u53ef\u7528\u7684\u5546\u54c1\u5217\u8868\u663e\u793a\u51fa\u6765\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u5546\u54c1\uff0c\u6211\u4eec\u5c06\u663e\u793a\u540d\u79f0\u3001\u4ef7\u683c\u548c\u201c\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66\u201d\u6309\u94ae\u3002<\/p>\n<p>&#8220;`html<\/p>\n<p>\n<p>\n<p>    \u5546\u54c1\u5217\u8868<\/p>\n<ul>\n<p>\n<li>\n<p><p>        {{ product.name }} &#8211; {{ product.price }}<\/p>\n<p>        <button>\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66<\/button><\/p>\n<\/li>\n<\/ul>\n<\/p>\n<\/p>\n<p>export default {<\/p>\n<p>  data() {<\/p>\n<p>    return {<\/p>\n<p>      products: [<\/p>\n<p>        { id: 1, name: &#8220;\u5546\u54c11&#8221;, price: 10 },<\/p>\n<p>        { id: 2, name: &#8220;\u5546\u54c12&#8221;, price: 15 },<\/p>\n<p>        { id: 3, name: &#8220;\u5546\u54c13&#8221;<\/p>\n<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.yimenyun.net\/2023-04\/06\/12\/16998060794.jpg\" \/><\/figure>\n<\/p>\n<p>, price: 20 },<\/p>\n<p>      ],<\/p>\n<p>    };<\/p>\n<p>  },<\/p>\n<p>  methods: {<\/p>\n<p>    addToCart(product) {<\/p>\n<p>      this.$emit(&#8220;add-to-cart&#8221;, product);<\/p>\n<p>    },<\/p>\n<p>  },<\/p>\n<p>};<\/p>\n<\/p>\n<p>&#8220;`<\/p>\n<p>### \u8d2d\u7269\u8f66\u7ec4\u4ef6<\/p>\n<p>\u8d2d\u7269\u8f66\u7ec4\u4ef6\u5c06\u4f1a\u662f\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u6838\u5fc3\u90e8\u5206\u3002\u5728\u8fd9\u4e2a\u7ec4\u4ef6\u4e2d\uff0c\u6211\u4eec\u5c06\u8ddf\u8e2a\u7528\u6237\u9009\u62e9\u7684\u5546\u54c1\uff0c\u5e76\u8ba1\u7b97\u8d2d\u7269\u8f66\u7684\u603b\u8ba1\u3002<\/p>\n<p>&#8220;`html<\/p>\n<p>\n<p>\n<p>    \u8d2d\u7269\u8f66<\/p>\n<ul>\n<p>\n<li>\n<p><p>        {{ product.name }} &#8211; {{ product.price }} x {{ product.quantity }}<\/p>\n<p>        <button>\u5220\u9664<\/button><\/p>\n<\/li>\n<\/ul>\n<p>    \u603b\u8ba1: {{ total }}<\/p>\n<\/p>\n<\/p>\n<p>export d<a href=\"https:\/\/www.zhidianwl.com\/\">\u667a\u7535\u745e\u521b<\/a>efault {<\/p>\n<p>  props: {<\/p>\n<p>    cart: { type: Array, default: () =&gt; [] },<\/p>\n<p>  },<\/p>\n<p>  computed: {<\/p>\n<p>    total() {<\/p>\n<p>      return this.cart.reduce((prev, curr) =&gt; prev + curr.price * curr.quantity, 0);<\/p>\n<p>    },<\/p>\n<p>  },<\/p>\n<p>  methods: {<\/p>\n<p>    removeFromCart(index) {<\/p>\n<p>      this.$emit(&#8220;remove-from-cart&#8221;, index);<\/p>\n<p>    },<\/p>\n<p>  },<\/p>\n<p>};<\/p>\n<\/p>\n<p>&#8220;`<\/p>\n<p>## Props<\/p>\n<p>\u6211\u4eec\u7684\u7ec4\u4ef6\u4e4b\u95f4\u9700\u8981\u76f8\u4e92\u901a\u4fe1\u3002\u5546\u54c1\u5217\u8868\u7ec4\u4ef6\u9700\u8981\u5c06\u88ab\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66\u7684\u5546\u54c1\u4f20\u9012\u7ed9\u8d2d\u7269\u8f66\u7ec4\u4ef6\u3002\u8d2d\u7269\u8f66\u7ec4\u4ef6\u9700\u8981\u5c06\u8d2d\u7269\u8f66\u7684\u5185\u5bb9\u4f20\u9012\u56de\u7236\u7ea7\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e9b\uff0c\u6211\u4eec\u4f7f\u7528 Vue \u7684 props \u6982\u5ff5\u3002props \u5141\u8bb8\u6211\u4eec\u5411\u4e00\u4e2a\u5b50\u7ec4\u4ef6\u4f20\u9012\u6570\u636e\u3002<\/p>\n<p>\u6211\u4eec\u5c06\u4f20\u9012\u4e24\u4e2a prop \u7ed9\u6211\u4eec\u7684\u8d2d\u7269\u8f66\u7ec4\u4ef6\uff1acart \u548c products\u3002cart \u5c5e\u6027\u5305\u542b\u7528\u6237\u5df2\u7ecf\u9009\u62e9\u7684\u5546\u54c1\uff0c\u800c products \u5c5e\u6027\u5305\u542b\u6240\u6709\u7684\u5546\u54c1\u5217\u8868\u3002<\/p>\n<p>&#8220;`html<\/p>\n<p>\n<p>\n<\/p>\n<\/p>\n<p>import ProductList from &#8220;.\/ProductList.vue&#8221;;<\/p>\n<p>import Cart from &#8220;.\/Cart.vue&#8221;;<\/p>\n<p>export default {<\/p>\n<p>  components: {<\/p>\n<p>    ProductList,<\/p>\n<p>    Cart,<\/p>\n<p>  },<\/p>\n<p>  data() {<\/p>\n<p>    return {<\/p>\n<p>      cart: [],<\/p>\n<p>    };<\/p>\n<p>  },<\/p>\n<p>  methods: {<\/p>\n<p>    addToCart(product) {<\/p>\n<p>      const index = this.cart.findIndex((p) =&gt; p.id === product.id);<\/p>\n<p>      if (index &lt; 0) {<\/p>\n<p>        this.cart.push({ &#8230;product, quantity: 1 });<\/p>\n<p>      } else {<\/p>\n<p>        this.cart[index].quantity++;<\/p>\n<p>      }<\/p>\n<p>    },<\/p>\n<p>    removeFromCart(index) {<\/p>\n<p>      this.cart.splice(index, 1);<\/p>\n<p>    },<\/p>\n<p>  },<\/p>\n<p>};<\/p>\n<\/p>\n<p>&#8220;`<\/p>\n<p>## \u4e8b\u4ef6<\/p>\n<p>\u7ec4\u4ef6\u4e4b\u95f4\u7684\u901a\u4fe1\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4e8b\u4ef6\u5b9e\u73b0\uff0c\u8fd9\u662f\u4e00\u79cd\u6211\u4eec\u53ef\u4ee5\u5728\u5b50\u7ec4\u4ef6\u4e2d\u53d1\u51fa\u7684\u4fe1\u53f7\uff0c\u7236\u7ea7\u7ec4\u4ef6\u53ef\u4ee5\u4fa6\u542c\u6b64\u4fe1\u53f7\u5e76\u91c7\u53d6\u76f8\u5e94\u7684\u884c\u52a8\u3002<\/p>\n<p>\u6211\u4eec\u901a\u8fc7\u5728\u5546\u54c1\u5217\u8868\u7ec4\u4ef6\u4e2d\u53d1\u51fa add-to-cart \u4e8b\u4ef6\u4ee5\u5c06\u5546\u54c1\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66\u4e2d\uff0c\u5e76\u5728\u8d2d\u7269\u8f66\u7ec4\u4ef6\u4e2d\u53d1\u51fa remove-from-cart \u4e8b\u4ef6\u4ee5\u4ece\u8d2d\u7269\u8f66\u4e2d\u5220\u9664\u5546\u54c1\u3002<\/p>\n<p>&#8220;`html<\/p>\n<p><button>\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66<\/button><\/p>\n<p>this.$emit(&#8220;add-to-cart&#8221;, product);<\/p>\n<p>&#8220;`<\/p>\n<p>&#8220;`html<\/p>\n<p><button>\u5220\u9664<\/button><\/p>\n<p>this.$emit(&#8220;remove-from-cart&#8221;, index);<\/p>\n<p>&#8220;`<\/p>\n<p>## \u8ba1\u7b97\u5c5e\u6027<\/p>\n<p>\u6211\u4eec\u7684\u8d2d\u7269\u8f66\u9700\u8981\u8ba1\u7b97\u5176\u603b\u8ba1\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 Vue \u7684\u53e6\u4e00\u4e2a\u6838\u5fc3\u6982\u5ff5\uff1a\u8ba1\u7b97\u5c5e\u6027\u3002<\/p>\n<p>\u8ba1\u7b97\u5c5e\u6027\u5141\u8bb8\u60a8\u5b9a\u4e49\u4e00\u4e2a\u57fa\u4e8e\u5176\u4ed6\u6570\u636e\u7684\u5c5e\u6027\uff0c\u8fd9\u6837\u6bcf\u5f53\u4f9d\u8d56\u9879\u4e2d\u7684\u6570\u636e\u66f4\u6539\u65f6\uff0c\u8ba1\u7b97\u5c5e\u6027\u5c06\u88ab\u91cd\u65b0\u8ba1\u7b97\u3002<\/p>\n<p>&#8220;`html<\/p>\n<p>\u603b\u8ba1: {{ total }}<\/p>\n<p>computed: {<\/p>\n<p>  total() {<\/p>\n<p>    return this.cart.reduce((prev, curr) =&gt; prev + curr.price * curr.quantity, 0);<\/p>\n<p>  },<\/p>\n<p>},<\/p>\n<p>&#8220;`<\/p>\n<p>## \u7ed3\u8bba<\/p>\n<p>\u4f7f\u7528 Vue \u6765\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u59cb\u7ec8\u662f\u4e00\u4ef6\u4ee4\u4eba\u6109\u60a6\u7684\u4e8b\u60c5\u3002Vue \u7684\u6e05\u6670 API\uff0c\u6613\u4e8e\u4f7f\u7528\u7684\u5de5\u5177\u96c6\u548c\u6e05\u6670\u7684\u6587\u6863\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u5feb\u901f\u9ad8\u6548\u5730\u6784\u5efa\u51fa\u4f18\u79c0\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528 Vue \u6765\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u8d2d\u7269\u8f66\uff0c\u4e86\u89e3\u4e86\u7ec4\u4ef6\uff0cprops\uff0c\u4e8b\u4ef6\u548c\u8ba1\u7b97\u5c5e\u6027\u7b49Vue \u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u8fd9\u4e9b\u6982\u5ff5\u5728\u6784\u5efa\u66f4\u5927\u578b\u548c\u66f4\u56f0\u96be\u7684\u5e94\u7528\u7a0b\u5e8f\u65f6\u5c06\u4f1a\u975e\u5e38\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vue \u662f\u4e00\u79cd\u5148\u8fdb\u7684 JavaScript \u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u4e3a\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u521b\u5efa\u4ea4\u4e92\u5f0f\u7528\u6237\u754c\u9762\u3002Vue \u8fd8\u5177\u6709\u73b0\u4ee3\u5316\u7684\u5de5\u5177\u96c6\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6784\u5efa\u66f4\u5feb\uff0c\u66f4\u9ad8\u6548\u7684 SPA \u5e94\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Vue \u521b<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[37791,291,39033,914,37962],"class_list":{"0":"post-50363","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-zuoapp","7":"tag-app","8":"tag-291","10":"tag-914","11":"tag-37962"},"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/50363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=50363"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/50363\/revisions"}],"predecessor-version":[{"id":50399,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/50363\/revisions\/50399"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=50363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=50363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=50363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}