{"id":268,"date":"2025-07-28T04:32:24","date_gmt":"2025-07-28T04:32:24","guid":{"rendered":"https:\/\/www.yehger.com\/?p=268"},"modified":"2025-07-28T04:33:06","modified_gmt":"2025-07-28T04:33:06","slug":"vue%e7%9a%84mvvm%ef%bc%8c%e5%92%8creact%e7%9a%84%e5%8d%95%e5%90%91%e6%95%b0%e6%8d%ae%e6%b5%81%ef%bc%8c%e4%bb%a5%e5%8f%8athinkphp%e7%9a%84mvc%e4%b9%8b%e9%97%b4%e7%9a%84%e5%8c%ba%e5%88%ab%e4%b8%8e","status":"publish","type":"post","link":"https:\/\/www.yehger.com\/index.php\/2025\/07\/28\/vue%e7%9a%84mvvm%ef%bc%8c%e5%92%8creact%e7%9a%84%e5%8d%95%e5%90%91%e6%95%b0%e6%8d%ae%e6%b5%81%ef%bc%8c%e4%bb%a5%e5%8f%8athinkphp%e7%9a%84mvc%e4%b9%8b%e9%97%b4%e7%9a%84%e5%8c%ba%e5%88%ab%e4%b8%8e\/","title":{"rendered":"Vue\u7684MVVM\u8be6\u89e3"},"content":{"rendered":"<h1>\u4ec0\u4e48\u662fMVVM\uff1f<\/h1>\n<p>MVVM = Model-View-ViewModel<\/p>\n<p>Vue\u7684MVVM\u4e25\u683c\u6765\u8bf4\u66f4\u50cf\u662f\u4e00\u4e2a\u62bd\u8c61\u6982\u5ff5\uff0cvue\u5e76\u4e0d\u662fMVVM\u67b6\u6784\u5b9e\u73b0\uff0c\u56e0\u4e3a\u5b83\u4e0d\u50cfThinkphp\u8fd9\u6837\u7684\u6846\u67b6\u5bf9\u4e8eMVC\u6709\u5177\u8c61\u7684\u8868\u73b0\uff0c\u53ea\u662f\u501f\u9274\u4e86MVVM\u7684\u6838\u5fc3\u601d\u60f3\u3002<\/p>\n<p>\u8fd9\u662f\u4e2a\u8001\u751f\u5e38\u8c08\u7684\u95ee\u9898\u4e86\uff0c\u793e\u533a\u91cc\u5bf9Vue\u662f\u5426\u662fMVVM\u67b6\u6784\u7684\u53eb\u6cd5\u95ee\u9898\u7ecf\u5e38\u4f1a\u6709\u4e0d\u540c\u7684\u89c2\u70b9\uff0c\u5176\u5b9e\u4f5c\u8005\u672c\u4eba\u4e5f\u5bf9\u8868\u8fbe\u4e86\u5bf9\u8fd9\u65b9\u9762\u7684\u89e3\u91ca<\/p>\n<h2>\u4ec0\u4e48\u662fModel\u548cView\uff1f<\/h2>\n<p>\u8fd9\u4e2a\u5176\u5b9e\u5f88\u597d\u7406\u89e3\uff0c\u5982\u679c\u6709MVC\u76f8\u5173\u7ecf\u9a8c\u7684\u8bdd<\/p>\n<p>Model\u603b\u7ed3\u4e0b\u6765\u5176\u5b9e\u5c31\u662f\u5bf9\u6570\u636e\u7684\u64cd\u4f5c\u7ba1\u7406\uff0c\u53ef\u80fd\u5305\u542b\uff1a<\/p>\n<ul>\n<li>\u6570\u636e\u6301\u4e45\u5316\u5b58\u50a8<\/li>\n<li>\u5bf9\u6570\u636e\u7684\u5904\u7406(\u683c\u5f0f\u5316\\\u65f6\u95f4\u6233\u5904\u7406\\\u5012\u8ba1\u65f6\u5904\u7406)<\/li>\n<li>\u4e0e\u540e\u7aef\u7684\u4ea4\u4e92<\/li>\n<li>\u5c01\u88c5\u4e0e\u7edf\u4e00\uff08\u4f8b\u5982\u4f7f\u7528vuex\u6216pinia\uff09<\/li>\n<li>\u5bf9view\u7684\u89e3\u8026\uff0c\u4f7f\u5f97\u6570\u636e\u5c42\u662f\u72ec\u7acb\u7684<\/li>\n<\/ul>\n<p>Model \u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-js\">\/\/\u8fd9\u4e2a\u5c31\u662fmodel\uff0c\u4e5f\u5c31\u662f\u6570\u636e\u5c42\nconst message = ref(&#039;Hello&#039;)\n\n\/\/\u4e5f\u53ef\u4ee5\u662freactive\nconst form = reactive({\n  user: {\n    name: &#039;&#039;,\n    age: 18\n  },\n  settings: {\n    theme: &#039;dark&#039;,\n    notifications: true\n  },\n  tags: [&#039;vue&#039;, &#039;mvvm&#039;, &#039;example&#039;]\n})\n\/\/\u53ef\u4ee5\u662fcomputed\nconst fullName = computed(() =&gt; `${firstName.value} ${lastName.value}`)\n\n\/\/\u751a\u81f3\u53ef\u4ee5\u662fPinia\u6216vuex\u7ba1\u7406\u7684\u6574\u4e2a\u6a21\u5757\nimport { defineStore } from &#039;pinia&#039;\nimport { ref, reactive } from &#039;vue&#039;\n\nexport const useProductStore = defineStore(&#039;product&#039;, () =&gt; {\n  \/\/ Model: \u5546\u54c1\u5217\u8868\n  const products = ref&lt;Product[]&gt;([])\n\n  \/\/ Model: \u5206\u9875\u4fe1\u606f\n  const pagination = reactive({\n    page: 1,\n    pageSize: 10,\n    total: 0\n  })\n\n  \/\/ Model: \u641c\u7d22\u6761\u4ef6\n  const searchKeyword = ref(&#039;&#039;)\n\n  \/\/ Action: \u4ece\u540e\u7aef\u52a0\u8f7d\u5546\u54c1\u5217\u8868\n  async function fetchProducts() {\n    const res = await fetch(`\/api\/products?page=${pagination.page}&amp;size=${pagination.pageSize}&amp;keyword=${searchKeyword.value}`)\n    const data = await res.json()\n    products.value = data.items\n    pagination.total = data.total\n  }\n\n  \/\/ Action: \u65b0\u589e\u5546\u54c1\n  async function addProduct(newProduct: Partial&lt;Product&gt;) {\n    await fetch(&#039;\/api\/products&#039;, {\n      method: &#039;POST&#039;,\n      body: JSON.stringify(newProduct)\n    })\n    await fetchProducts() \/\/ \u5237\u65b0\u5217\u8868\n  }\n\n  return {\n    products, pagination, searchKeyword,\n    fetchProducts, addProduct\n  }\n})<\/code><\/pre>\n<p>View\u76f8\u8f83\u4e8eModel\u66f4\u5bb9\u6613\u7406\u89e3\u4e00\u4e9b\uff0cModel\u5305\u542b\u4e00\u4e9b\u62bd\u8c61\u6982\u5ff5\uff0c\u800cView\u7eaf\u7cb9\u662f\u5177\u8c61\u7684\u8868\u73b0<br \/>\n\u53ef\u4ee5\u8fd9\u6837\u7406\u89e3\uff0c\u6240\u6709\u6807\u8bb0\u8bed\u8a00\u7684\u90e8\u5206\u57fa\u672c\u90fd\u5c5e\u4e8eview<br \/>\n\u5305\u62ec\u7ec4\u4ef6\u4e5f\u662f<\/p>\n<pre><code class=\"language-js\">\/\/\u6574\u4e2atemplate\u90e8\u5206\u90fd\u662fview\uff0c\u89c6\u56fe\u5c42\n&lt;template&gt;\n  &lt;input v-model=&quot;message&quot; \/&gt;\n  &lt;p&gt;{{ message }}&lt;\/p&gt;\n&lt;\/template&gt;<\/code><\/pre>\n<h2>\u4ec0\u4e48\u662fViewModel\u5462\uff1f<\/h2>\n<p>ViewModel\u76f8\u5bf9\u6765\u8bf4\u6bd4\u8f83\u96be\u7406\u89e3\u4e00\u4e9b\uff0c\u56e0\u4e3a\u5355\u770b\u8fd9\u4e2aView Model\u7684\u5355\u8bcd\u4f1a\u89c9\u5f97\u8fd9\u662f\u4e2a\u62bd\u8c61\u6982\u5ff5\uff0c\u4f46\u5b9e\u9645\u4e0aViewModel\u786e\u5b9e\u5c31\u662f\u4e00\u4e2a\u62bd\u8c61\u6d41\u7a0b<\/p>\n<p>ViewModel = \u5e2e\u52a9 View \u83b7\u53d6\u548c\u64cd\u4f5c Model \u7684\u4e2d\u4ecb\u6216\u201c\u6865\u6881\u201d\uff0c\u8d1f\u8d23\u534f\u8c03\u6570\u636e\u7684\u8bfb\u53d6\u4e0e\u66f4\u65b0\u6d41\u7a0b\uff0c\u5b9e\u73b0 View \u548c Model \u4e4b\u95f4\u7684\u89e3\u8026\u4e0e\u540c\u6b65\u6d41\u7a0b<\/p>\n<pre><code class=\"language-js\">\/\/\u5177\u4f53\u8868\u73b0\u5982\u4e0b\nViewModel\u5305\u542bAction\uff0c\u4e0eModel\u5b58\u5728\u5173\u7cfb\uff0c\u4e0eView\u4e5f\u5b58\u5728\u5173\u7cfb\n&lt;script setup&gt;\nimport { ref } from &#039;vue&#039;\n\nconst count = ref(0)      \/\/ \u72b6\u6001\uff08Model\uff09\nfunction increment() {    \/\/ \u64cd\u4f5c\uff08Action\uff09\n  count.value++\n}\n&lt;\/script&gt;\n&lt;template&gt;\n  &lt;button @click=&quot;increment&quot;&gt;+1&lt;\/button&gt;\n  &lt;p&gt;{{ count }}&lt;\/p&gt;\n&lt;\/template&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662fMVVM\uff1f MVVM = Model-View-ViewModel Vue\u7684MVVM\u4e25\u683c\u6765\u8bf4\u66f4\u50cf\u662f\u4e00\u4e2a\u62bd\u8c61\u6982\u5ff5\uff0cvue\u5e76\u4e0d\u662fMVVM\u67b6\u6784\u5b9e\u73b0\uff0c\u56e0\u4e3a\u5b83\u4e0d\u50cfThinkphp\u8fd9\u6837\u7684\u6846\u67b6\u5bf9\u4e8eMVC\u6709\u5177\u8c61\u7684\u8868\u73b0\uff0c\u53ea\u662f\u501f\u9274\u4e86MVVM\u7684\u6838\u5fc3\u601d\u60f3\u3002 \u8fd9\u662f\u4e2a\u8001\u751f\u5e38\u8c08\u7684\u95ee\u9898\u4e86\uff0c\u793e\u533a\u91cc\u5bf9Vue\u662f\u5426\u662fMVVM\u67b6\u6784\u7684\u53eb\u6cd5\u95ee\u9898\u7ecf\u5e38\u4f1a\u6709\u4e0d\u540c\u7684\u89c2\u70b9\uff0c\u5176\u5b9e\u4f5c\u8005\u672c\u4eba\u4e5f\u5bf9\u8868\u8fbe\u4e86\u5bf9\u8fd9\u65b9\u9762\u7684\u89e3\u91ca \u4ec0\u4e48\u662fModel\u548cView\uff1f \u8fd9\u4e2a\u5176\u5b9e\u5f88\u597d\u7406\u89e3\uff0c\u5982\u679c\u6709MVC\u76f8\u5173\u7ecf\u9a8c\u7684\u8bdd Model\u603b\u7ed3\u4e0b\u6765\u5176\u5b9e\u5c31\u662f\u5bf9\u6570\u636e\u7684\u64cd\u4f5c\u7ba1\u7406\uff0c\u53ef\u80fd\u5305\u542b\uff1a \u6570\u636e\u6301\u4e45\u5316\u5b58\u50a8 \u5bf9\u6570\u636e\u7684\u5904\u7406(\u683c\u5f0f\u5316\\\u65f6\u95f4\u6233\u5904\u7406\\\u5012\u8ba1\u65f6\u5904\u7406) \u4e0e\u540e\u7aef\u7684\u4ea4\u4e92 \u5c01\u88c5\u4e0e\u7edf\u4e00\uff08\u4f8b\u5982\u4f7f\u7528vuex\u6216pinia\uff09 \u5bf9view\u7684\u89e3\u8026\uff0c\u4f7f\u5f97\u6570\u636e\u5c42\u662f\u72ec\u7acb\u7684 Model \u793a\u4f8b\uff1a \/\/\u8fd9\u4e2a\u5c31\u662fmodel\uff0c\u4e5f\u5c31\u662f\u6570\u636e\u5c42 const message =&#8230; <a class=\"read-more\" href=\"https:\/\/www.yehger.com\/index.php\/2025\/07\/28\/vue%e7%9a%84mvvm%ef%bc%8c%e5%92%8creact%e7%9a%84%e5%8d%95%e5%90%91%e6%95%b0%e6%8d%ae%e6%b5%81%ef%bc%8c%e4%bb%a5%e5%8f%8athinkphp%e7%9a%84mvc%e4%b9%8b%e9%97%b4%e7%9a%84%e5%8c%ba%e5%88%ab%e4%b8%8e\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-268","post","type-post","status-publish","format-standard","hentry","category-vue"],"_links":{"self":[{"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/posts\/268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/comments?post=268"}],"version-history":[{"count":2,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/posts\/268\/revisions"}],"predecessor-version":[{"id":270,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/posts\/268\/revisions\/270"}],"wp:attachment":[{"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/media?parent=268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/categories?post=268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/tags?post=268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}