{"id":17841,"date":"2025-06-17T13:20:20","date_gmt":"2025-06-17T05:20:20","guid":{"rendered":"https:\/\/92it.top\/?p=17841"},"modified":"2025-06-17T13:20:20","modified_gmt":"2025-06-17T05:20:20","slug":"%e5%89%8d%e5%90%8e%e7%ab%af%e5%88%86%e7%a6%bb%e7%9a%84%e4%b8%80%e7%82%b9tips","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=17841","title":{"rendered":"\u524d\u540e\u7aef\u5206\u79bb\u7684\u4e00\u70b9Tips"},"content":{"rendered":"\n<p>\u5728\u524d\u540e\u7aef\u5206\u79bb\u7684\u67b6\u6784\u4e2d\uff0c<strong>\u524d\u7aef\u9875\u9762\u4e3b\u8981\u901a\u8fc7 JavaScript \u52a8\u6001\u6e32\u67d3<\/strong>\u3002\u8fd9\u79cd\u6a21\u5f0f\u4e0e\u4f20\u7edf\u7684\u670d\u52a1\u7aef\u6e32\u67d3\uff08\u5982 JSP\u3001Thymeleaf\uff09\u6709\u672c\u8d28\u533a\u522b\uff0c\u6838\u5fc3\u6d41\u7a0b\u662f\uff1a<\/p>\n\n\n\n<ol>\n<li><strong>\u540e\u7aef\u4e13\u6ce8 API<\/strong>\uff1a\u53ea\u63d0\u4f9b RESTful API \u63a5\u53e3\uff0c\u8fd4\u56de JSON\/XML \u6570\u636e\uff0c\u4e0d\u8d1f\u8d23\u9875\u9762\u6e32\u67d3\u3002<\/li>\n\n\n\n<li><strong>\u524d\u7aef\u72ec\u7acb\u8fd0\u884c<\/strong>\uff1a\u901a\u8fc7 AJAX\u3001Fetch \u7b49\u65b9\u5f0f\u8c03\u7528\u540e\u7aef API\uff0c\u83b7\u53d6\u6570\u636e\u540e<strong>\u52a8\u6001\u751f\u6210 DOM \u5143\u7d20<\/strong>\uff0c\u5b8c\u6210\u9875\u9762\u5c55\u793a\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u4e00\u3001\u524d\u7aef\u6e32\u67d3\u7684\u6838\u5fc3\u6280\u672f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>1.&nbsp;<strong>\u539f\u751f JavaScript<\/strong><\/p>\n\n\n\n<p>\u901a\u8fc7&nbsp;<code>document.createElement()<\/code>\u3001<code>innerHTML<\/code>&nbsp;\u7b49\u539f\u751f API \u64cd\u4f5c DOM\u3002<br><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!-- \u9875\u9762\u7ed3\u6784 -->\n&lt;div id=\"user-list\">&lt;\/div>\n\n&lt;script>\n  \/\/ \u83b7\u53d6\u6570\u636e\n  fetch('\/api\/users')\n    .then(res => res.json())\n    .then(users => {\n      \/\/ \u52a8\u6001\u6e32\u67d3\n      const container = document.getElementById('user-list');\n      users.forEach(user => {\n        const div = document.createElement('div');\n        div.innerHTML = `&lt;p>${user.name}&lt;\/p>`;\n        container.appendChild(div);\n      });\n    });\n&lt;\/script><\/pre>\n\n\n\n<p><strong>2.\u00a0\u524d\u7aef\u6846\u67b6\uff08React\/Vue\/Angular\uff09<\/strong><\/p>\n\n\n\n<p>\u6846\u67b6\u901a\u8fc7\u865a\u62df DOM \u548c\u7ec4\u4ef6\u5316\u601d\u60f3\uff0c\u7b80\u5316\u590d\u6742 UI \u7684\u6e32\u67d3\u903b\u8f91\u3002<br><strong>\u793a\u4f8b\uff08React\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function UserList() {\n  const [users, setUsers] = useState([]);\n\n  useEffect(() => {\n    fetch('\/api\/users')\n      .then(res => res.json())\n      .then(setUsers);\n  }, []);\n\n  return (\n    &lt;div>\n      {users.map(user => (\n        &lt;p key={user.id}>{user.name}&lt;\/p>\n      ))}\n    &lt;\/div>\n  );\n}<\/pre>\n\n\n\n<p><strong>3.\u00a0\u6a21\u677f\u5f15\u64ce\uff08Handlebars\/Mustache\uff09<\/strong><\/p>\n\n\n\n<p>\u901a\u8fc7\u9884\u5b9a\u4e49\u6a21\u677f\u548c\u6570\u636e\uff0c\u751f\u6210 HTML \u5b57\u7b26\u4e32\u3002<br><strong>\u793a\u4f8b\uff08Handlebars\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;script id=\"user-template\" type=\"text\/x-handlebars-template\">\n  {{#each users}}\n    &lt;p>{{name}}&lt;\/p>\n  {{\/each}}\n&lt;\/script>\n\n&lt;script>\n  const template = Handlebars.compile($('#user-template').html());\n  fetch('\/api\/users')\n    .then(res => res.json())\n    .then(data => {\n      $('#user-list').html(template(data));\n    });\n&lt;\/script><\/pre>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u4e8c\u3001\u4e0e\u670d\u52a1\u7aef\u6e32\u67d3\u7684\u5bf9\u6bd4<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>\u7279\u6027<\/th><th>\u524d\u7aef\u6e32\u67d3\uff08JS\uff09<\/th><th>\u670d\u52a1\u7aef\u6e32\u67d3\uff08\u5982 Thymeleaf\uff09<\/th><\/tr><\/thead><tbody><tr><td><strong>\u6e32\u67d3\u65f6\u673a<\/strong><\/td><td>\u6d4f\u89c8\u5668\u4e0b\u8f7d HTML + JS \u2192 \u6267\u884c JS \u2192 \u751f\u6210 DOM<\/td><td>\u670d\u52a1\u5668\u751f\u6210\u5b8c\u6574 HTML \u2192 \u6d4f\u89c8\u5668\u76f4\u63a5\u5c55\u793a<\/td><\/tr><tr><td><strong>\u9996\u5c4f\u52a0\u8f7d\u901f\u5ea6<\/strong><\/td><td>\u8f83\u6162\uff08\u9700\u7b49\u5f85 JS \u52a0\u8f7d\u548c\u6267\u884c\uff09<\/td><td>\u8f83\u5feb\uff08\u76f4\u63a5\u62ff\u5230\u5b8c\u6574 HTML\uff09<\/td><\/tr><tr><td><strong>SEO \u53cb\u597d\u5ea6<\/strong><\/td><td>\u5dee\uff08\u641c\u7d22\u5f15\u64ce\u722c\u866b\u53ef\u80fd\u65e0\u6cd5\u83b7\u53d6\u5b8c\u6574\u5185\u5bb9\uff09<\/td><td>\u597d\uff08\u670d\u52a1\u5668\u8fd4\u56de\u5b8c\u6574 HTML\uff09<\/td><\/tr><tr><td><strong>\u4ea4\u4e92\u4f53\u9a8c<\/strong><\/td><td>\u597d\uff08\u9875\u9762\u5c40\u90e8\u66f4\u65b0\uff0c\u65e0\u5237\u65b0\u611f\uff09<\/td><td>\u4e00\u822c\uff08\u6bcf\u6b21\u8bf7\u6c42\u53ef\u80fd\u5237\u65b0\u6574\u4e2a\u9875\u9762\uff09<\/td><\/tr><tr><td><strong>\u5f00\u53d1\u6a21\u5f0f<\/strong><\/td><td>\u524d\u540e\u7aef\u5b8c\u5168\u5206\u79bb\uff0c\u53ef\u72ec\u7acb\u5f00\u53d1<\/td><td>\u524d\u540e\u7aef\u5f3a\u8026\u5408\uff0c\u4f9d\u8d56\u670d\u52a1\u7aef\u73af\u5883<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u4e09\u3001\u524d\u7aef\u6e32\u67d3\u7684\u4f18\u5316\u65b9\u6848<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>1.\u00a0\u9884\u6e32\u67d3\uff08Prerendering\uff09<\/strong><\/p>\n\n\n\n<p>\u5728\u6784\u5efa\u65f6\u751f\u6210\u9759\u6001 HTML \u6587\u4ef6\uff0c\u89e3\u51b3\u9996\u5c4f\u52a0\u8f7d\u548c SEO \u95ee\u9898\u3002<\/p>\n\n\n\n<ul>\n<li>\u5de5\u5177\uff1a<a href=\"https:\/\/github.com\/chrisvfritz\/prerender-spa-plugin\" target=\"_blank\" rel=\"noreferrer noopener\">prerender-spa-plugin<\/a>\uff08Vue\/React\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>2.\u00a0\u670d\u52a1\u7aef\u6e32\u67d3\uff08SSR\uff09<\/strong><\/p>\n\n\n\n<p>\u7ed3\u5408\u524d\u7aef\u6846\u67b6\u548c Node.js \u670d\u52a1\u5668\uff0c\u5728\u670d\u52a1\u5668\u7aef\u6267\u884c JS \u5e76\u751f\u6210 HTML\u3002<\/p>\n\n\n\n<ul>\n<li>\u6280\u672f\uff1aReact\uff08Next.js\uff09\u3001Vue\uff08Nuxt.js\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>3.\u00a0\u61d2\u52a0\u8f7d\uff08Lazy Loading\uff09<\/strong><\/p>\n\n\n\n<p>\u5c06\u975e\u5173\u952e\u8d44\u6e90\uff08\u5982\u7ec4\u4ef6\u3001\u56fe\u7247\uff09\u5ef6\u8fdf\u52a0\u8f7d\uff0c\u63d0\u5347\u9996\u5c4f\u901f\u5ea6\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ React \u61d2\u52a0\u8f7d\u793a\u4f8b\nconst UserProfile = React.lazy(() => import('.\/UserProfile'));\n\nfunction App() {\n  return (\n    &lt;React.Suspense fallback={&lt;div>Loading...&lt;\/div>}>\n      &lt;UserProfile \/>\n    &lt;\/React.Suspense>\n  );\n}<\/pre>\n\n\n\n<p><strong>4.\u00a0CDN \u52a0\u901f<\/strong><\/p>\n\n\n\n<p>\u901a\u8fc7 CDN \u5206\u53d1\u9759\u6001\u8d44\u6e90\uff08JS\u3001CSS\uff09\uff0c\u51cf\u5c11\u670d\u52a1\u5668\u538b\u529b\u3002<\/p>\n\n\n\n<p> \u3000<\/p>\n\n\n\n<p><strong>\u4e94\u3001\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u524d\u540e\u7aef\u5206\u79bb\u7684\u6838\u5fc3\u662f&nbsp;<strong>\u201c\u540e\u7aef\u63d0\u4f9b\u6570\u636e\uff0c\u524d\u7aef\u8d1f\u8d23\u6e32\u67d3\u201d<\/strong>\uff0cJavaScript \u662f\u5b9e\u73b0\u8fd9\u4e00\u6a21\u5f0f\u7684\u5173\u952e\u6280\u672f\u3002\u867d\u7136\u524d\u7aef\u6e32\u67d3\u5b58\u5728\u9996\u5c4f\u52a0\u8f7d\u6162\u3001SEO \u4e0d\u53cb\u597d\u7b49\u95ee\u9898\uff0c\u4f46\u901a\u8fc7 SSR\u3001\u9884\u6e32\u67d3\u7b49\u65b9\u6848\u53ef\u4ee5\u6709\u6548\u5f25\u8865\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u9700\u6839\u636e\u4e1a\u52a1\u9700\u6c42\uff08\u5982\u4ea4\u4e92\u590d\u6742\u5ea6\u3001SEO \u4f18\u5148\u7ea7\uff09\u9009\u62e9\u5408\u9002\u7684\u6e32\u67d3\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u524d\u540e\u7aef\u5206\u79bb\u7684\u67b6\u6784\u4e2d\uff0c\u524d\u7aef\u9875\u9762\u4e3b\u8981\u901a\u8fc7 JavaScript \u52a8\u6001\u6e32\u67d3\u3002\u8fd9\u79cd\u6a21\u5f0f\u4e0e\u4f20\u7edf\u7684\u670d\u52a1\u7aef\u6e32\u67d3\uff08\u5982 JSP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17841"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17841"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17841\/revisions"}],"predecessor-version":[{"id":17842,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17841\/revisions\/17842"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}