{"id":1454,"date":"2024-11-10T10:16:52","date_gmt":"2024-11-10T02:16:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1454\/"},"modified":"2024-11-10T10:16:52","modified_gmt":"2024-11-10T02:16:52","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-vue-3-%e4%b8%ad%e8%8e%b7%e5%8f%96%e5%85%83%e7%b4%a0%e7%9a%84-margin-top-%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1454\/","title":{"rendered":"\u5982\u4f55\u5728 Vue 3 \u4e2d\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173043815817488.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 Vue 3 \u4e2d\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 Vue 3 \u4e2d\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728  3 \u4e2d\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c<\/strong><\/p>\n<p>\u60f3\u8981\u5b9e\u73b0\u52a8\u753b\u6548\u679c\uff0c\u9700\u8981\u52a8\u6001\u83b7\u53d6 dom \u5143\u7d20\u7684 margin-top \u503c\u3002\u64cd\u4f5c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p><strong>\u786e\u4fdd dom \u5143\u7d20\u5df2\u7ecf\u6e32\u67d3\u5b8c\u6bd5<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u76ee\u6807\u5143\u7d20\u5df2\u7ecf\u5728 mount \u65f6\u6e32\u67d3\u5b8c\u6bd5\u3002\u53ef\u4ee5\u5728 mounted() \u94a9\u5b50\u51fd\u6570\u4e2d\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u5229\u7528 composition api \u83b7\u53d6\u503c<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 composition api \u83b7\u53d6 margin-top \u503c\uff1a<\/p>\n<pre>&lt;div ref=\"targetdom\"&gt;&lt;\/idv&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>\/\/ \u8fd9\u91cc\u4e0d\u7528\u4f20\u503c\uff0c\u53d8\u91cf\u540d\u8981\u7b49\u4e8e ref\nconst targetDom = ref&lt;HTMLElement&gt;()\n\nfunction getMarginTop(): string {\n  const styles = getComputedStyle(targetDom.value);\n  return styles.getPropertyValue('marginTop');\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Vue 3 \u4e2d\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c\uff1f\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>\u5728 3 \u4e2d\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c \u60f3\u8981\u5b9e\u73b0\u52a8\u753b\u6548\u679c\uff0c\u9700\u8981\u52a8\u6001\u83b7\u53d6 dom \u5143\u7d20\u7684 margin-top \u503c\u3002\u64cd\u4f5c\u6b65\u9aa4\u5982\u4e0b\uff1a \u786e\u4fdd dom \u5143\u7d20\u5df2\u7ecf\u6e32\u67d3\u5b8c\u6bd5 \u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u76ee\u6807\u5143\u7d20\u5df2\u7ecf\u5728 mount \u65f6\u6e32\u67d3\u5b8c\u6bd5\u3002\u53ef\u4ee5\u5728 mounted() \u94a9\u5b50\u51fd\u6570\u4e2d\u8fdb\u884c\u64cd\u4f5c\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5229\u7528 composition api \u83b7\u53d6\u503c \u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 composition api \u83b7\u53d6 margin-top \u503c\uff1a &lt;div ref=&#8221;targetdom&#8221;&gt;&lt;\/idv&gt; \u767b\u5f55\u540e\u590d\u5236 \/\/ \u8fd9\u91cc\u4e0d\u7528\u4f20\u503c\uff0c\u53d8\u91cf\u540d\u8981\u7b49\u4e8e ref const targetDom = ref&lt;HTMLElement&gt;() function getMarginTop(): string { const styles = getComputedStyle(targetDom.value); return styles.getPropertyValue(&#8216;marginTop&#8217;); } \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Vue 3 \u4e2d\u83b7\u53d6\u5143\u7d20\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1454","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1454","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=1454"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1454\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}