{"id":194,"date":"2024-05-04T15:14:19","date_gmt":"2024-05-04T07:14:19","guid":{"rendered":"http:\/\/www.shifengzhi.asia\/?p=194"},"modified":"2024-05-04T15:14:19","modified_gmt":"2024-05-04T07:14:19","slug":"%e7%ae%80%e5%8d%95%e6%89%8b%e6%92%95%e5%ae%9e%e7%8e%b0vue%e7%9a%84%e5%93%8d%e5%ba%94%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/boki.shifengzhi.top\/?p=194","title":{"rendered":"\u7b80\u5355\u624b\u6495\u5b9e\u73b0vue\u7684\u54cd\u5e94\u5f0f"},"content":{"rendered":"<p>\u4f7f\u7528\u8fc7 vue \u670b\u53cb\u90fd\u77e5\u9053 vue \u662f\u6570\u636e\u9a71\u52a8\u89c6\u56fe\uff0c\u5b9e\u73b0\u53cc\u5411\u6570\u636e\u7ed1\u5b9a\uff0c\u6570\u636e\u53d8\u5316\u754c\u9762\u4e5f\u8ddf\u7740\u53d8\u5316\uff0c\u4eca\u5929\u6211\u4eec\u6765\u6d45\u6d45\u5730\u5f53\u4e00\u4e0b\u5c0f\u5c0f\u7684\u5c24\u96e8\u6eaa\uff0c\u56de\u5230\u90a3\u4e2a\u6ca1\u6709 vue \u7684\u65f6\u5019\uff0c\u7528\u539f\u751f js \u5b9e\u73b0\u4e00\u4e0b vue \u54cd\u5e94\u5f0f<\/p>\n<h3>\u6750\u6599\u51c6\u5907<\/h3>\n<p>\u5148\u7b80\u5355\u5199\u4e2a\u9875\u9762\u548c\u6837\u5f0f<\/p>\n<pre><code>\/\/ index.html\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n  &lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot; \/&gt;\n    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; \/&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; \/&gt;\n    &lt;title&gt;Vue\u54cd\u5e94\u5f0f&lt;\/title&gt;\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;.\/index.css&quot; \/&gt;\n    &lt;script src=&quot;.\/yyx.js&quot;&gt;&lt;\/script&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;div class=&quot;card&quot;&gt;\n      &lt;p id=&quot;lastName&quot;&gt;&lt;\/p&gt;\n      &lt;p id=&quot;firstName&quot;&gt;&lt;\/p&gt;\n      &lt;p id=&quot;age&quot;&gt;&lt;\/p&gt;\n    &lt;\/div&gt;\n    &lt;input type=&quot;text&quot; oninput=&quot;user.name = this.value&quot; \/&gt;\n    &lt;input type=&quot;date&quot; onchange=&quot;user.birth = this.value&quot; \/&gt;\n    &lt;script src=&quot;.\/index.js&quot;&gt;&lt;\/script&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<pre><code>\/\/ index.css\n.card {\n  width: 300px;\n  border: 2px solid rgb(74, 125, 142);\n  border-radius: 10px;\n  font-size: 2em;\n  padding: 0 20px;\n  margin: 0 auto;\n  background: lightblue;\n  color: #333;\n}\ninput[type=&quot;text&quot;] {\n  margin-left: 600px;\n}<\/code><\/pre>\n<h3>\u5b9e\u73b0\u54cd\u5e94\u5f0f<\/h3>\n<p>\u6211\u4eec\u521b\u5efa\u4e00\u4e2a js \u6587\u4ef6\uff0c\u6a21\u62df vue \u7684 js \u6e90\u4ee3\u7801\u6587\u4ef6\uff0c\u5c31\u8d77\u540d\u53eb <code>yyx.js<\/code><\/p>\n<p>\u8be6\u7ec6\u4ee3\u7801\u5982\u4e0b\uff0c\u8be6\u7ec6\u4e86\u89e3\u8fc7 vue \u7684\u670b\u53cb\u80af\u5b9a\u542c\u8fc7<strong>\u4f9d\u8d56\u6536\u96c6<\/strong>\u548c<strong>\u6d3e\u53d1\u66f4\u65b0<\/strong><\/p>\n<pre><code>\/\/ yyx.js\n\/**\n * \u5b9e\u73b0\u54cd\u5e94\u5f0f\u7684\u51fd\u6570\uff0c\u5728\u5bf9\u5e94\u7684\u503c\u7684 get \u548c set \u4e2d\u62ff\u5230\u4f9d\u8d56\u5c5e\u6027\u7684\u51fd\u6570\u5e76\u6267\u884c\n * @param {Object} obj \n *\/\nfunction respond(obj) {\n  for (const key in obj) {\n    let internalValue = obj[key]\n    let funcs = new Set()\n    Object.defineProperty(obj, key, {\n      get: function () {\n        \/\/  \u4f9d\u8d56\u6536\u96c6\uff1a\u6536\u96c6\u8be5\u5c5e\u6027\u88ab\u54ea\u4e9b\u51fd\u6570\u4f9d\u8d56\n        if (window.__func) {\n          funcs.add(window.__func)\n        }\n        return internalValue\n      },\n      set: function (value) {\n        \/\/ \u6d3e\u53d1\u66f4\u65b0\uff1a\u5f53\u5c5e\u6027\u503c\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u901a\u77e5\u6240\u6709\u4f9d\u8d56\u8be5\u5c5e\u6027\u7684\u51fd\u6570\u6267\u884c\n        internalValue = value\n        funcs.forEach(func =&gt; func())\n      }\n    })\n  }\n}\n\/**\n * \u4f7f\u7528\u4e00\u4e2a\u5927\u5168\u5c40\u53d8\u91cf\u63a5\u6536\u8c03\u7528\u7684\u51fd\u6570\uff0c\u4ee5\u4fbf\u5728 get \u4e2d\u77e5\u9053\u662f\u54ea\u4e2a\u51fd\u6570\u4f9d\u8d56\u5bf9\u5e94\u5c5e\u6027\u8c03\u7528\uff0c\u5e76\u5728 set \u4e2d\u6267\u884c\u8be5\u51fd\u6570\n * @param {function} fn \n *\/\nfunction autoRunFunc(fn) {\n  window.__func = fn\n  fn()\n  window.__func = null\n}<\/code><\/pre>\n<p>\u9898\u5916\u8bdd\uff1a\u8fd9\u91cc\u7684 window.__func \u6709\u70b9 PHP \u7684\u8d85\u5168\u5c40\u53d8\u91cf\u7684\u610f\u5473\uff0c\u96be\u602a\u90fd\u8bf4\u201cPHP\u662f\u4e16\u754c\u4e0a\u6700\u597d\u7684\u8bed\u8a00\u201d\uff08\u7b11\uff09<\/p>\n<p>\u6709\u4e86\u6211\u4eec\u624b\u5199\u7684\u6846\u67b6\u6587\u4ef6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u81ea\u5df1\u8981\u5199\u7684 js \u91cc\u4f7f\u7528\u4e86\uff0c\u6700\u540e\u770b\u4e00\u4e0b\u6548\u679c<\/p>\n<pre><code>\/\/ index.js\nlet user = {\n  name: &#039;\u4e16\u98ce\u81f3&#039;,\n  birth: &#039;2000-01-01&#039;,\n}\n\/* \u4f20\u5165\u5bf9\u8c61\uff0c\u4f7f\u5176\u53d8\u4e3a\u54cd\u5e94\u5f0f *\/\nrespond(user)\n\/\/ \u663e\u793a\u59d3\u6c0f\nfunction showLastName() {\n  document.querySelector(&#039;#lastName&#039;).textContent = &#039;\u59d3\uff1a&#039; + user.name[0]\n}\n\/\/ \u663e\u793a\u540d\u5b57\nfunction showFirstName() {\n  document.querySelector(&#039;#firstName&#039;).textContent = &#039;\u540d\uff1a&#039; + user.name.slice(1)\n}\n\/\/ \u663e\u793a\u5e74\u9f84\nfunction showAge() {\n  let birthday = new Date(user.birth)\n  let today = new Date()\n  today.setHours(0), today.setMinutes(0), today.setMilliseconds(0)\n  let thisYearBirthday = new Date(\n    today.getFullYear(),\n    birthday.getMonth(),\n    birthday.getDate()\n  )\n  let age = today.getFullYear() - birthday.getFullYear()\n  if (today.getTime() &lt; thisYearBirthday.getTime()) {\n    age--\n  }\n  document.querySelector(&#039;#age&#039;).textContent = &#039;\u5e74\u9f84\uff1a&#039; + age\n}\n\n\/\/ showLastName()\n\/\/ showFirstName()\n\/\/ showAge()\n\n\/* \u8c03\u7528autoRunFunc\u51fd\u6570\uff0c\u4ee5\u4fbf\u83b7\u5f97\u8981\u6267\u884c\u7684\u51fd\u6570 *\/\nautoRunFunc(showFirstName)\nautoRunFunc(showLastName)\nautoRunFunc(showAge)<\/code><\/pre>\n<p>\u8fd9\u91cc\u5e74\u9f84\u51fd\u6570\u662f\u7528\u65e5\u671f\u7b97\u51fa\u6765\u7684\uff0c\u4e0d\u7528\u5728\u610f\uff0c\u4f60\u4e5f\u53ef\u4ee5\u76f4\u63a5\u7528\u8f93\u5165\u6846\u8f93\u5165\u5e74\u9f84\uff0c\u770b\u61c2\u4e86\u4e5f\u53bb\u52a8\u624b\u5b9e\u73b0\u4e00\u4e0b\u5427<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u8fc7 vue \u670b\u53cb\u90fd\u77e5\u9053 vue \u662f\u6570\u636e\u9a71\u52a8\u89c6\u56fe\uff0c\u5b9e\u73b0\u53cc\u5411\u6570\u636e\u7ed1\u5b9a\uff0c\u6570\u636e\u53d8\u5316\u754c\u9762\u4e5f\u8ddf\u7740\u53d8\u5316\uff0c\u4eca\u5929\u6211\u4eec\u6765\u6d45\u6d45\u5730 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[24],"class_list":["post-194","post","type-post","status-publish","format-standard","hentry","category-5","tag-vue"],"_links":{"self":[{"href":"https:\/\/boki.shifengzhi.top\/index.php?rest_route=\/wp\/v2\/posts\/194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/boki.shifengzhi.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boki.shifengzhi.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boki.shifengzhi.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boki.shifengzhi.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=194"}],"version-history":[{"count":1,"href":"https:\/\/boki.shifengzhi.top\/index.php?rest_route=\/wp\/v2\/posts\/194\/revisions"}],"predecessor-version":[{"id":195,"href":"https:\/\/boki.shifengzhi.top\/index.php?rest_route=\/wp\/v2\/posts\/194\/revisions\/195"}],"wp:attachment":[{"href":"https:\/\/boki.shifengzhi.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boki.shifengzhi.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boki.shifengzhi.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}