{"id":8019,"date":"2023-11-28T20:11:09","date_gmt":"2023-11-28T12:11:09","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=8019"},"modified":"2023-12-02T21:10:01","modified_gmt":"2023-12-02T13:10:01","slug":"vue-zhong-ru-he-rangcss-zhi-zai-dang-qian-zu-jian","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/vue-zhong-ru-he-rangcss-zhi-zai-dang-qian-zu-jian\/","title":{"rendered":"Vue \u4e2d\u5982\u4f55\u8ba9 CSS \u53ea\u5728\u5f53\u524d\u7ec4\u4ef6\u4e2d\u8d77\u4f5c\u7528\uff1f"},"content":{"rendered":"<p>\u5728 Vue \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6280\u672f\u6765\u786e\u4fdd CSS \u53ea\u5728\u5f53\u524d\u7ec4\u4ef6\u4e2d\u8d77\u4f5c\u7528\uff0c\u800c\u4e0d\u5f71\u54cd\u5176\u4ed6\u7ec4\u4ef6\u7684\u6837\u5f0f\u3002<\/p>\n<ol>\n<li>\n<p><strong>Scoped CSS\uff08\u4f5c\u7528\u57df\u6837\u5f0f\uff09<\/strong>\uff1a<\/p>\n<ul>\n<li>Vue \u63d0\u4f9b\u4e86 <code>&lt;style scoped&gt;<\/code> \u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u8ba9 CSS \u6837\u5f0f\u4ec5\u5728\u5f53\u524d\u7ec4\u4ef6\u4e2d\u751f\u6548\u3002\u8fd9\u6837\u505a\u7684\u539f\u7406\u662f Vue \u4f1a\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u54c8\u5e0c\u503c\uff0c\u5c06\u8fd9\u4e2a\u54c8\u5e0c\u503c\u4f5c\u4e3a\u7c7b\u540d\u7684\u4e00\u90e8\u5206\uff0c\u4ece\u800c\u786e\u4fdd\u6837\u5f0f\u53ea\u5728\u5f53\u524d\u7ec4\u4ef6\u7684 DOM \u5143\u7d20\u4e0a\u751f\u6548\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-vue\">&lt;template&gt;\n  &lt;div class=&quot;container&quot;&gt;\n    &lt;h1&gt;Scoped CSS Example&lt;\/h1&gt;\n  &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\nexport default {\n  \/\/ ...\u7ec4\u4ef6\u903b\u8f91\n};\n&lt;\/script&gt;\n\n&lt;style scoped&gt;\n.container {\n  background-color: lightblue;\n  padding: 20px;\n}\n\nh1 {\n  color: blue;\n}\n&lt;\/style&gt;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>.container<\/code> \u7c7b\u7684\u6837\u5f0f\u548c <code>h1<\/code> \u6807\u7b7e\u7684\u6837\u5f0f\u53ea\u4f1a\u5728\u5f53\u524d\u7ec4\u4ef6\u4e2d\u751f\u6548\uff0c\u4e0d\u4f1a\u6cc4\u9732\u5230\u5176\u4ed6\u7ec4\u4ef6\u4e2d\u3002<\/p>\n<\/li>\n<li>\n<p><strong>CSS Modules<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 CSS Modules \u4e5f\u662f\u53e6\u4e00\u79cd\u63a7\u5236\u6837\u5f0f\u4f5c\u7528\u57df\u7684\u65b9\u5f0f\u3002\u901a\u8fc7 Vue CLI \u521b\u5efa\u7684\u9879\u76ee\u53ef\u4ee5\u76f4\u63a5\u5728 <code>&lt;style&gt;<\/code> \u6807\u7b7e\u4e2d\u4f7f\u7528 CSS Modules\uff0c\u5b83\u4f1a\u81ea\u52a8\u5c06 CSS \u6837\u5f0f\u9650\u5236\u5728\u5f53\u524d\u7ec4\u4ef6\u7684\u8303\u56f4\u5185\uff0c\u907f\u514d\u6837\u5f0f\u6c61\u67d3\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-vue\">&lt;template&gt;\n  &lt;div class=&quot;container&quot;&gt;\n    &lt;h1&gt;CSS Modules Example&lt;\/h1&gt;\n  &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\n\/\/ \u5bfc\u5165 CSS \u6a21\u5757\nimport styles from '.\/YourComponent.module.css';\n\nexport default {\n  \/\/ ...\u7ec4\u4ef6\u903b\u8f91\n};\n&lt;\/script&gt;\n\n&lt;style module&gt;\n\/* \u4f7f\u7528 CSS Modules *\/\n.container {\n  \/* \u5f15\u7528 CSS \u6a21\u5757 *\/\n  composes: container from '.\/YourComponent.module.css';\n}\n\nh1 {\n  \/* \u5f15\u7528 CSS \u6a21\u5757 *\/\n  composes: heading from '.\/YourComponent.module.css';\n}\n&lt;\/style&gt;\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd CSS \u6837\u5f0f\u53ea\u5728\u5f53\u524d\u7ec4\u4ef6\u5185\u8d77\u4f5c\u7528\uff0c\u907f\u514d\u5168\u5c40\u6c61\u67d3\u548c\u6837\u5f0f\u51b2\u7a81\uff0c\u63d0\u9ad8\u7ec4\u4ef6\u5316\u5f00\u53d1\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Vue \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6280\u672f\u6765\u786e\u4fdd CSS \u53ea\u5728\u5f53\u524d\u7ec4\u4ef6\u4e2d\u8d77\u4f5c\u7528\uff0c\u800c\u4e0d\u5f71\u54cd\u5176\u4ed6\u7ec4\u4ef6\u7684\u6837\u5f0f\u3002 Scoped  [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114],"tags":[],"class_list":["post-8019","post","type-post","status-publish","format-standard","hentry","category-vue"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=8019"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8019\/revisions"}],"predecessor-version":[{"id":14021,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8019\/revisions\/14021"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=8019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=8019"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=8019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}