{"id":29681,"date":"2024-11-25T11:04:59","date_gmt":"2024-11-25T03:04:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/29681\/"},"modified":"2024-11-25T11:04:59","modified_gmt":"2024-11-25T03:04:59","slug":"%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e5%bc%80%e5%8f%91%e4%b8%80%e5%91%a8%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/29681\/","title":{"rendered":"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e00\u5468\u603b\u7ed3"},"content":{"rendered":"<p><strong>\u4ee5\u4e0b\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e00\u5468\u603b\u7ed3\uff0c\u5e0c\u671b\u5bf9\u7c73\u4e91\u7684\u540c\u5b66\u6709\u5e2e\u52a9\u3002<\/strong><\/p>\n<p><strong>\u7f16\u8f91\u5668<\/strong><\/p>\n<p>\u6211\u76f4\u63a5\u7528 vscode\uff08\u5176\u5b83\u7f16\u8f91\u5668\u540c\u7406\uff0c\u9884\u89c8\u8fd8\u662f\u7528\u7684\u5fae\u4fe1\u5f00\u53d1\u5de5\u5177\uff09\uff0c\u8bed\u6cd5\u9ad8\u4eae\u5c06 wxml \u8bbe\u7f6e\u6210 html, wxss \u8bbe\u7f6e\u6210 css<\/p>\n<pre>\"files.associations\": {\r\n  \"*.wxss\": \"css\",\r\n  \"*.wxml\": \"html\"\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>&nbsp;\u4e5f\u53ef\u4ee5\u5b89\u88c5\u5c0f\u7a0b\u5e8f\u76f8\u5173\u63d2\u4ef6<\/p>\n<p><strong>\u5f00\u59cb\u5199\u4ee3\u7801<\/strong><\/p>\n<ul>\n<li>\n<p>\u9996\u5148\u9700\u8981\u5b8c\u6574\u770b\u5b8c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u6587\u6863\uff08\uff0c\u7ec4\u4ef6\u548c \uff09\uff0c\u65b9\u4fbf\u540e\u9762\u7528\u5230\u65f6\u67e5\u627e\u3002<\/p>\n<\/li>\n<li>\n<p>view \u7ec4\u4ef6\u5bf9\u5e94 html \u91cc\u7684 p<\/p>\n<\/li>\n<li>\n<p>text \u5bf9\u5e94 span<\/p>\n<\/li>\n<li>\n<p>wxss \u91cc\u9009\u62e9\u5668\u53ea\u652f\u6301 element, #id, .className, ::after, ::before<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u516c\u7528\u7ec4\u4ef6<\/strong><\/p>\n<p>\u9879\u76ee\u76ee\u5f55\u91cc\u65b0\u5efa components \uff0c\u6309\u7c7b\u4f3c pages \uff0c\u5c06\u6bcf\u4e2a\u7ec4\u4ef6\u7684\u6a21\u677f\uff0c\u6837\u5f0f\u548c js \u6587\u4ef6\u653e\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u5939<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/001\/505\/39400ef3cadbe5fabd414f0b88d4dfe2-0.jpg\" class=\"aligncenter\" title=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e00\u5468\u603b\u7ed3\u63d2\u56fe\" alt=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e00\u5468\u603b\u7ed3\u63d2\u56fe\" \/><\/p>\n<ul>\n<li>\n<p>\u6a21\u677f\u53ef\u4ee5\u76f4\u63a5 \u6216\u8005\u9700\u8981\u4f20\u503c\u7684\u4f7f\u7528 + <\/p>\n<\/li>\n<li>\n<p>\u6837\u5f0f\u4f7f\u7528 @import \u5bfc\u5165<\/p>\n<\/li>\n<li>\n<p>js \u4f7f\u7528  \u5f15\u5165\u5230\u9875\u9762\uff0c\u7136\u540e\u4f7f\u7528\u4e0b\u9762\u7684 mergePage \u6765\u52a0\u8f7d\u5230\u9875\u9762\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><strong>mergePage<\/strong><\/p>\n<ul>\n<li>\n<p>\u7ec4\u4ef6\u7684\u52a0\u8f7d<\/p>\n<\/li>\n<\/ul>\n<pre>const ErrorMsg = require('..\/..\/..\/components\/error-msg\/error-msg');\r\nPage(util.mergePage({\r\n  \/\/ \u9875\u9762 Page \u65b9\u6cd5...\r\n  onLoad() {\r\n    \/\/ \u53ef\u4ee5\u76f4\u63a5\u5728\u9875\u9762\u65b9\u6cd5\u4e2d\u8c03\u7528 showErrorMsg \u65b9\u6cd5\r\n  }\r\n}, ErrorMsg\/* \u66f4\u591a\u7ec4\u4ef6\u4e5f\u53ef\u4ee5*\/));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>&nbsp;<\/p>\n<p>\u4f7f\u7528 mergePage \u65b9\u6cd5\u5c06\u6240\u6709\u7ec4\u4ef6\u65b9\u6cd5\u53ca\u9875\u9762\u6ce8\u518c\u5230\u9875\u9762\u5bf9\u8c61<\/p>\n<ul>\n<li>\n<p>\u7ec4\u4ef6\u7684\u7f16\u5199\u65b9\u5f0f<\/p>\n<\/li>\n<\/ul>\n<pre>var errorTimer;\r\n\r\nmodule.exports = {\r\n  showErrorMsg(msg, cb) {\r\n    clearTimeout(errorTimer);\r\n    this.setData({\r\n      errorMsg: msg\r\n    });\r\n    errorTimer = setTimeout( () =&gt; {\r\n      this.setData({\r\n        errorMsg: false\r\n      });\r\n      cb &amp;&amp; cb();\r\n    }, 2000);\r\n  }\r\n  \/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u6ce8\u518c `onLoad`\uff0c`onShow`\u7b49\u9875\u9762\u4e8b\u4ef6\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>&nbsp;<\/p>\n<p>\u7ec4\u4ef6\u91cc\u4f7f\u7528\u53ef\u4ee5 this.setData \u6765\u66f4\u65b0\u9875\u9762\u6570\u636e\uff0c\u6216\u8005\u6ce8\u518c onLoad \uff0c onShow \u7b49\u9875\u9762\u4e8b\u4ef6\uff0c mergePage \u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u7684\u4e8b\u4ef6\u4f1a\u6700\u5148\u8c03\u7528\u3002<\/p>\n<ul>\n<li>\n<p>mergePage \u7684\u6e90\u7801<\/p>\n<\/li>\n<\/ul>\n<pre>\/**\r\n * \u5408\u5e76 Page \u5bf9\u8c61\u6240\u6709\u7684\u65b9\u6cd5\u53ca\u4e8b\u4ef6\r\n * \u5b50\u5bf9\u8c61\u4e0d\u80fd\u4f7f\u7528 data \u5c5e\u6027\uff0c\u8bf7\u5728 onLoad \u4e2d\u4f7f\u7528 setData \u65b9\u6cd5\u8bbe\u7f6e\r\n *\/\r\nfunction mergePage(dest, ...src) {\r\n  let args = arguments;\r\n  let eventsStack = {\r\n    onLoad: [],\r\n    onReady: [],\r\n    onShow: [],\r\n    onHide: [],\r\n    onUnload: [],\r\n    onPullDownRefresh: [],\r\n    onReachBottom: [],\r\n  };\r\n  \/\/ \u4fdd\u5b58\u6240\u6709\u7684\u4e8b\u4ef6\uff0c\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u7684\u4e8b\u4ef6\u4f1a\u6700\u5148\u8c03\u7528\u3002\r\n  for(let name in eventsStack) {\r\n    for(let i = args.length - 1; i &gt;= 0; i--) {\r\n      args[i][name] &amp;&amp; eventsStack[name].push(args[i][name])\r\n    }\r\n  }\r\n  Object.assign(...args);\r\n  for(let name in eventsStack) {\r\n    dest[name] = function() {\r\n      for(let i = 0; i &lt; eventsStack[name].length; i++) {\r\n        eventsStack[name][i].apply(this, arguments);\r\n      }\r\n    }\r\n  }\r\n  return dest;\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>&nbsp;<\/p>\n<h2><strong>es 6<\/strong><\/h2>\n<h3><strong>\u7bad\u5934\u51fd\u6570\uff0c\u9ed8\u8ba4\u503c\u53ca\u89e3\u6790\u6784<\/strong><\/h3>\n<pre>wx.request({\r\n  complete: ({data= {}}) =&gt; {\r\n    \/\/ 1. \u56e0\u4e3a wx.request \u8fd4\u56de\u7684\u63a5\u53e3\u6570\u636e\u662f\u5728 data \u5c5e\u6027\u91cc\uff0c\u8fd9\u91cc\u6211\u4eec\u53ea\u8981 data \u5c5e\u6027\u5c31\u884c\u4e86\uff0c\u6240\u4ee5\u76f4\u63a5\u53c2\u6570\u89e3\u6790\u6784\r\n    \/\/ 2. \u5982\u679c failed\uff0c\u65e0 data \u65f6\uff0cdata \u5c06\u4e3a\u9ed8\u8ba4\u503c {}\r\n    if(data.code !== 0) {\r\n      \/\/ do something if request failed\r\n      return;\r\n    }\r\n    \/\/ \u8bf7\u6c42\u6b63\u5e38\u5904\u7406\u4ee3\u7801\r\n    \/\/ 3. \u56e0\u4e3a\u7528\u7684\u7bad\u5934\u51fd\u6570\uff0c\u56de\u8c03\u91cc\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528 this, \u8bbf\u95ee Page \u5bf9\u8c61\u7684\u65b9\u6cd5\r\n    \/\/ \u6bd4\u5982 this.setData(...)\r\n  }\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>&nbsp;<\/p>\n<p>\u4e00\u4e9b\u51fd\u6570\u53c2\u6570\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u9ed8\u8ba4\u53c2\u6570\u3002<\/p>\n<h3><strong>\u62d3\u5c55 \u548c \u5bf9\u8c61\u5c5e\u6027\u7b80\u5199<\/strong><\/h3>\n<p>\u5728\u7ed9 template \u4f20 data \u53c2\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5bf9\u8c61\u5c5e\u6027\u7b80\u5199\uff0c\u5982<\/p>\n<pre>&lt;template is=\"...\" data=\"{{...obj, id: otherIdVariable, name}}\"&gt;&lt;\/template&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u6837 template \u4e2d\u53ef\u4ee5\u4f7f\u7528\u4e3a obj \u5bf9\u8c61\u7684\u6240\u6709 \uff0c\u4ee5\u53ca id \u548c name<\/p>\n<h3><strong>\u6a21\u677f<\/strong><\/h3>\n<p>\u5c0f\u7a0b\u5e8f\u91cc\u53ef\u4ee5\u76f4\u63a5\u65b9\u4fbf\u7684\u4f7f\u7528 es 6 \u6a21\u677f\u5b57\u7b26\u4e32<\/p>\n<pre>let url = `${app.globalData.API_PREFIX}\/cart\/add`;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>&nbsp;<\/p>\n<h3><strong>\u66f4\u591a es 6 \u7279\u6027<\/strong><\/h3>\n<h2><strong>\u5176\u5b83\u6ce8\u610f<\/strong><\/h2>\n<ul>\n<li>\n<p>wx.showToast \u56fe\u6807\u53ea\u652f\u6301&#8221;success&#8221;\u3001&#8221;loading&#8221;\uff0c\u9519\u8bef\u63d0\u793a\u5f97\u81ea\u5b9a\u4e49<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c template \u91cc\u9762\u7684\u53d8\u91cf\u6ca1\u503c\uff0c\u8bf7\u770b data \u4f20\u8fdb\u6765\u6ca1\u6709\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f00\u53d1\u5de5\u5177\uff08v0.10.102800\uff09\u53ef\u4ee5\u7528\u4e0b\u9762\u65b9\u6cd5\u6dfb\u52a0\u63a5\u53e3\u8bf7\u6c42\u57df\u540d\uff0c\u9057\u61be\u7684\u662f\u5fae\u4fe1\u91cc\u4e0d\u884c\u3002<\/p>\n<\/li>\n<\/ul>\n<pre>\/\/ \u653e\u5230 app.js \u524d\u9762\r\n wxConfig.projectConfig.Network.RequestDomain.push('https:\/\/weapp.juanpi.com');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>\n<p>\u6240\u6709\u9875\u9762\u7684 JS \u4f1a\u5728\u542f\u52a8\u65f6\u7acb\u5373\u6267\u884c\uff0c\u800c\u4e0d\u662f\u6253\u5f00\u9875\u9762\u624d\u6267\u884c\uff0c\u6240\u4ee5\u4e00\u4e9b\u5199\u5728\u5168\u5c40\u7684\u4ee3\u7801\u5e94\u8be5\u5c3d\u91cf\u653e\u5230 onLoad \u4e4b\u540e\uff0c\u4e0b\u9762\u662f\u4ece source \u91cc\u770b\u5230\u52a0\u8f7d\u7684\u4ee3\u7801\uff1a<\/p>\n<\/li>\n<\/ul>\n<pre>define(\"pages\/index\/index.js\", function(require, module, exports, window,document,frames,self,location,navigator,localStorage,history,Caches,screen,alert,confirm,prompt,XMLHttpRequest,WebSocket ){ 'use strict';\r\n\r\nvar app = getApp();\r\n\r\nvar util = require('..\/..\/..\/utils\/util');\r\nvar ErrorMsg = require('..\/..\/..\/components\/error-msg\/error-msg');\r\nvar AddressPicker = require('..\/..\/..\/components\/address-picker\/address-picker');\r\n\r\nPage(util.mergePage({\r\n  \/\/ \u9875\u9762\u4ee3\u7801\u7701\u7565\r\n}, AddressPicker, ErrorMsg));\r\n\/\/# sourceMappingURL=data:application\/json;...\r\n});require(\"pages\/index\/index.js\")<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e00\u5468\u603b\u7ed3\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e00\u5468\u603b\u7ed3\uff0c\u5e0c\u671b\u5bf9\u7c73\u4e91\u7684\u540c\u5b66\u6709\u5e2e\u52a9\u3002 \u7f16\u8f91\u5668 \u6211\u76f4\u63a5\u7528 vscode\uff08\u5176\u5b83\u7f16\u8f91\u5668\u540c\u7406\uff0c\u9884\u89c8\u8fd8\u662f\u7528\u7684\u5fae\u4fe1\u5f00\u53d1\u5de5\u5177\uff09\uff0c\u8bed\u6cd5\u9ad8\u4eae\u5c06 wxml \u8bbe\u7f6e\u6210 html, wxss \u8bbe\u7f6e\u6210 css &#8220;files.associations&#8221;: { &#8220;*.wxss&#8221;: &#8220;css&#8221;, &#8220;*.wxml&#8221;: &#8220;html&#8221; } \u767b\u5f55\u540e\u590d\u5236 &nbsp;\u4e5f\u53ef\u4ee5\u5b89\u88c5\u5c0f\u7a0b\u5e8f\u76f8\u5173\u63d2\u4ef6 \u5f00\u59cb\u5199\u4ee3\u7801 \u9996\u5148\u9700\u8981\u5b8c\u6574\u770b\u5b8c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u6587\u6863\uff08\uff0c\u7ec4\u4ef6\u548c \uff09\uff0c\u65b9\u4fbf\u540e\u9762\u7528\u5230\u65f6\u67e5\u627e\u3002 view \u7ec4\u4ef6\u5bf9\u5e94 html \u91cc\u7684 p text \u5bf9\u5e94 span wxss \u91cc\u9009\u62e9\u5668\u53ea\u652f\u6301 element, #id, .className, ::after, ::before \u516c\u7528\u7ec4\u4ef6 \u9879\u76ee\u76ee\u5f55\u91cc\u65b0\u5efa components \uff0c\u6309\u7c7b\u4f3c pages \uff0c\u5c06\u6bcf\u4e2a\u7ec4\u4ef6\u7684\u6a21\u677f\uff0c\u6837\u5f0f\u548c js \u6587\u4ef6\u653e\u5728\u540c\u4e00\u4e2a\u6587\u4ef6\u5939 \u6a21\u677f\u53ef\u4ee5\u76f4\u63a5 \u6216\u8005\u9700\u8981\u4f20\u503c\u7684\u4f7f\u7528 + \u6837\u5f0f\u4f7f\u7528 @import \u5bfc\u5165 js \u4f7f\u7528 \u5f15\u5165\u5230\u9875\u9762\uff0c\u7136\u540e\u4f7f\u7528\u4e0b\u9762\u7684 mergePage [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-29681","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/29681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=29681"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/29681\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=29681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=29681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=29681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}