{"id":792,"date":"2024-07-26T17:07:52","date_gmt":"2024-07-26T09:07:52","guid":{"rendered":"https:\/\/levin.ink\/?p=792"},"modified":"2024-09-08T17:20:05","modified_gmt":"2024-09-08T09:20:05","slug":"vue-5-2-vue-%e8%84%9a%e6%89%8b%e6%9e%b6","status":"publish","type":"post","link":"https:\/\/levin.ink\/?p=792","title":{"rendered":"VUE &#8211; 6-2 VUE \u811a\u624b\u67b6"},"content":{"rendered":"<h1>\u4f7f\u7528\u811a\u624b\u67b6\u642d\u5efa\u5de5\u7a0b<\/h1>\n<h2>vue-cli<\/h2>\n<p>vue-cli\u96c6\u6210\u4e86\u8bb8\u591a\u524d\u7aef\u6280\u672f\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a<\/p>\n<ul>\n<li>webpack<\/li>\n<li>babel<\/li>\n<li>eslint<\/li>\n<li>http-proxy-middleware<\/li>\n<li>typescript<\/li>\n<li>css pre-prosessor<\/li>\n<li>css module<\/li>\n<li>...<br \/>\n\u8fd9\u4e9b\u5de5\u5177\u5927\u90e8\u5206\u90fd\u4f9d\u8d56\u4e8enode\u73af\u5883\u548cnpm<\/li>\n<\/ul>\n<h2>vue-cli \u4f7f\u7528<\/h2>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff0c\u6839\u636e\u63d0\u793a\u521b\u5efa\u5373\u53ef<\/p>\n<pre><code class=\"language-shell\">vue creat \u9879\u76ee\u540d\u79f0\n<\/code><\/pre>\n<h2>\u9879\u76ee\u7ed3\u6784<\/h2>\n<pre><code class=\"language-plaintext\">bili-app\n\u251c\u2500 .gitignore \n\u251c\u2500 babel.config.js\n\u251c\u2500 jsconfig.json\n\u251c\u2500 node_modules\n\u2502  \u2514\u2500 ...\n\u251c\u2500 package-lock.json\n\u251c\u2500 package.json\n\u251c\u2500 public\n\u2502  \u251c\u2500 favicon.ico\n\u2502  \u2514\u2500 index.html\n\u251c\u2500 README.md\n\u251c\u2500 src\n\u2502  \u251c\u2500 App.vue\n\u2502  \u251c\u2500 assets\n\u2502  \u2502  \u2514\u2500 logo.png\n\u2502  \u251c\u2500 components\n\u2502  \u2502  \u2514\u2500 HelloWorld.vue\n\u2502  \u2514\u2500 main.js\n\u2514\u2500 vue.config.js\n<\/code><\/pre>\n<h1>\u5f00\u53d1<\/h1>\n<h2>\u829d\u58eb<\/h2>\n<h3>\u63d2\u69fd<\/h3>\n<pre><code class=\"language-html\">&lt;template&gt;\n    &lt;div class=&quot;item&quot;&gt;\n        &lt;slot&gt;&lt;\/slot&gt;\n    &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;item&gt;\n    &lt;h1&gt;Hello Bilibili&lt;\/h1&gt;\n&lt;\/item&gt;\n<\/code><\/pre>\n<p><code>item<\/code>\u6807\u7b7e\u4e2d\u7684\u5185\u5bb9\u4f1a\u88ab\u653e\u5230<code>slot<\/code>\u6807\u7b7e\u5904\u3002<\/p>\n<h3>\u5177\u540d\u63d2\u69fd<\/h3>\n<pre><code class=\"language-html\">&lt;template&gt;\n    &lt;div class=&quot;item&quot;&gt;\n        &lt;div class=&quot;left&quot;&gt;\n            &lt;slot name=&quot;title&quot;&gt;&lt;\/slot&gt;\n        &lt;\/div&gt;\n        &lt;div class=&quot;right&quot;&gt;\n            &lt;slot name=&quot;icon&quot;&gt;&lt;\/slot&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;item&gt;\n    &lt;template v-slot:title&gt;xxx&lt;\/template&gt;\n    &lt;template v-slot:icon&gt;&lt;img src=&quot;&quot; \/&gt;&lt;\/template&gt;\n&lt;\/item&gt;\n<\/code><\/pre>\n<h3>\u52a8\u6001\u7ed1\u5b9a\u7c7b\u6837\u5f0f<\/h3>\n<pre><code class=\"language-html\">&lt;template&gt;\n    &lt;div class=&quot;item&quot; :class=&quot;{active: isActive}&quot;&gt;\n        &lt;slot&gt;&lt;\/slot&gt;\n    &lt;\/div&gt;\n&lt;\/template&gt;\n<\/code><\/pre>\n<p>\u901a\u8fc7\u7ed9<code>:class<\/code>\u4f20\u5165\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5c5e\u6027\u540d\u662f\u9700\u8981\u52a8\u6001\u6dfb\u52a0\u7684\u6837\u5f0f\uff0c\u5c5e\u6027\u503c\u7684<code>true<\/code>\u6216<code>false<\/code>\u5f71\u54cd\u5c5e\u6027\u540d\u7684\u6837\u5f0f\u662f\u5426\u6dfb\u52a0\uff0c\u5982\u679c\u5c5e\u6027\u503c\u7ed3\u679c\u4e3a<code>true<\/code>\u5219\u6dfb\u52a0\u5bf9\u5e94\u7c7b\u6837\u5f0f\u3002<\/p>\n<pre><code class=\"language-html\">&lt;template&gt;\n    &lt;div class=&quot;item&quot; :class=&quot;isExpand ? 'style1' : 'style2'&quot;&gt;\n        &lt;slot&gt;&lt;\/slot&gt;\n    &lt;\/div&gt;\n&lt;\/template&gt;\n<\/code><\/pre>\n<p>\u4e5f\u53ef\u901a\u8fc7\u4e09\u76ee\u8868\u8fbe\u5f0f<\/p>\n<h3>\u52a8\u6001\u7ed1\u5b9astyle<\/h3>\n<pre><code class=\"language-html\">&lt;template&gt;\n    &lt;div class=&quot;item&quot; :style=&quot;{\n        width: width + 'px',\n        height: height + 'px'\n    }&quot;&gt;\n        &lt;slot&gt;&lt;\/slot&gt;\n    &lt;\/div&gt;\n&lt;\/template&gt;\n<\/code><\/pre>\n<p>\u5f53\u7136\u4e5f\u53ef\u4ee5\u5199\u4e00\u4e2a\u8ba1\u7b97\u5c5e\u6027\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;template&gt;\n    &lt;div class=&quot;item&quot; :style=&quot;style&quot;&gt;\n        &lt;slot&gt;&lt;\/slot&gt;\n    &lt;\/div&gt;\n&lt;\/template&gt;\n<\/code><\/pre>\n<blockquote>\n<p>\u82e5\u5c5e\u6027\u540d\u542b\u6709&quot;-&quot;\u4e4b\u7c7b\u7684\u6bd4\u5982<code>margin-top<\/code>\uff0c\u5c06\u5176\u4f7f\u7528\u5f15\u53f7\u5305\u88f9<\/p>\n<\/blockquote>\n<pre><code class=\"language-js\">export default {\n    computed: {\n        style() {\n            return {\n                'margin-top': '3px',\n                width: width + 'px',\n                height: height + 'px'\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h3>\u7ea6\u675f\u5c5e\u6027<\/h3>\n<p>\u5982\u4f55\u907f\u514d\u5411\u4f7f\u7528\u7ec4\u4ef6\u65f6\u4f20\u5165\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5982\u4f55\u4fdd\u8bc1\u4f20\u5165\u5bf9\u5e94\u6570\u636e\u5462\uff1f<\/p>\n<pre><code class=\"language-js\">export default {\n    props: {\n        isActive: {\n            type: Boolean, \/\/ \u7ea6\u675f\u5c5e\u6027\u7c7b\u578b\n            required: true, \/\/ \u7ea6\u675f\u5c5e\u6027\u662f\u5426\u5fc5\u987b\u4f20\u9012\n            \/\/default: false, \/\/ \u9ed8\u8ba4\u503c\n        },\n    }\n};\n<\/code><\/pre>\n<p>\u82e5\u4f20\u5165\u7684\u503c\u4e0d\u662f<code>Boolean<\/code>\u7c7b\u578b\uff0c\u5c06\u4f1a\u5f97\u5230\u4ee5\u4e0b\u8b66\u544a\uff1a<\/p>\n<pre><code class=\"language-plaintext\">[Vue warn]: Invalid prop: type check failed for prop &quot;isActive&quot;. Expected Boolean, got String with value &quot;true&quot;.\n<\/code><\/pre>\n<blockquote>\n<p>\u82e5\u9ed8\u8ba4\u503c\u4e3a\u5bf9\u8c61\uff0c\u5fc5\u987b\u4f7f\u7528\u51fd\u6570\uff0c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e3a\u5bf9\u8c61\u3002<br \/>\n\u6211\u4eec\u5728\u914d\u7f6edata\u65f6\u540c\u6837\u4f7f\u7528\u51fd\u6570\uff0c\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u3002<br \/>\n\u539f\u56e0\uff0c\u7ec4\u4ef6\u53ef\u80fd\u88ab\u4f7f\u7528\u591a\u6b21\uff0c\u82e5\u4e0d\u5199\u6210\u51fd\u6570\uff0c\u90a3\u4e48\u591a\u4e2a\u7ec4\u4ef6\u4e4b\u95f4\u4f1a\u5171\u4eab\u4e00\u4e2a\u5bf9\u8c61\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528\u51fd\u6570\uff0c\u6570\u636e\u6765\u7740\u51fd\u6570\u8c03\u7528\uff0c\u8fd4\u56de\u7684\u4e0d\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4fdd\u8bc1\u7ec4\u4ef6\u4e4b\u95f4\u72ec\u7acb\u3002<\/p>\n<\/blockquote>\n<h3>\u56fe\u6807\u5e93\uff0c\u56fe\u6807\u7ec4\u4ef6<\/h3>\n<pre><code class=\"language-html\">&lt;template&gt;\n    &lt;i class=&quot;iconfont&quot; :class=&quot;iconClass&quot;&gt;&lt;\/i&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\n\/\/ \u6620\u5c04\u5173\u7cfb\nlet iconMap = {\n    'big-left': 'icon-chevron_big_left',\n    'big-right': 'icon-chevron_big_right',\n    'big-up': 'icon-chevron_big_up',\n    'big-down': 'icon-chevron_big_down',\n    ...\n\n};\n\nexport default {\n    props: {\n        type: {\n            type: String,\n            required: true,\n        }\n    },\n    computed: {\n        iconClass() {\n            return iconMap[this.type];\n        }\n    }\n};\n&lt;\/script&gt;\n\n&lt;style scoped&gt;\n\/\/ \u5f15\u5165css\n@import &quot;\/\/at.alicdn.com\/t\/c\/font_4633489_dx291zwcocm.css&quot;;\n&lt;\/style&gt;\n<\/code><\/pre>\n<p>\u4f7f\u7528<\/p>\n<pre><code class=\"language-html\">&lt;icon :type=&quot;'big-right'&quot;&gt;&lt;\/icon&gt;\n<\/code><\/pre>\n<h2>\u95ee\u9898<\/h2>\n<h3>\u7c7b\u6837\u5f0f\u51b2\u7a81<\/h3>\n<p>\u5f53\u6211\u4eec\u5f00\u53d1\u7ec4\u4ef6\u65f6\u5982\u4f55\u9632\u6b62\u7c7b\u6837\u5f0f\u51b2\u7a81<\/p>\n<pre><code class=\"language-html\">&lt;template&gt;\n    &lt;div class=&quot;item&quot;&gt;item components&lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;style scoped&gt;\n.item {\n    cursor: pointer;\n}\n\n.item:hover {\n    background: #f4f4f4;\n}\n&lt;\/style&gt;\n<\/code><\/pre>\n<p>\u6211\u4eec\u7ed9style\u52a0\u4e0a\u4e00\u4e2ascoped\u6807\u7b7e\u540e\uff0c\u751f\u6210\u7684\u5143\u7d20\u5982\u4e0b<\/p>\n<pre><code class=\"language-html\">&lt;div data-v-186c01a3 class=&quot;item&quot;&gt;item components&lt;\/div&gt;\n<\/code><\/pre>\n<p>\u5bf9\u5e94\u7684css\u9009\u62e9\u5668\u5982\u4e0b<\/p>\n<pre><code class=\"language-css\">.item[data-v-186c01a3] {\n    cursor: pointer;\n}\n<\/code><\/pre>\n<p>\u4f1a\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u81ea\u5b9a\u4e49\u5c5e\u6027\uff0c\u907f\u514d\u7c7b\u6837\u5f0f\u51b2\u7a81\uff0c\u501f\u9274\u4e8eCSS module\u7684\u505a\u6cd5\u3002<\/p>\n<h3>\u76d2\u5b50\u5bbd\u9ad8\u5982\u4f55\u5904\u7406<\/h3>\n<p>\u5916\u90e8\u4f7f\u7528\u7ec4\u4ef6\u65f6\u53ef\u80fd\u4f1a\u4f7f\u7528\u4e0d\u540c\u7684\u5bbd\u9ad8\uff0c\u9488\u5bf9\u8fd9\u4e2a\u95ee\u9898\uff0c\u5c06\u7ec4\u4ef6\u7684\u8bbe\u7f6e\u4e3a\u6491\u6ee1\u7236\u5143\u7d20<\/p>\n<pre><code class=\"language-css\">.item {\n    width: 100%;\n    height: 100%;\n}\n<\/code><\/pre>\n<h3>\u4e8b\u4ef6\u4e0e\u6570\u636e<\/h3>\n<p>\u6570\u636e\u662f\u7236\u7ec4\u4ef6\u7684\uff0c\u4e8b\u4ef6\u662f\u5b50\u7ec4\u4ef6\u7684\uff0c\u7236\u5b50\u7ec4\u4ef6\u4e4b\u95f4\u9700\u8981\u901a\u4fe1<br \/>\n\u5b50\u7ec4\u4ef6\u53ef\u4ee5\u629b\u51fa\u4e8b\u4ef6\uff1a<\/p>\n<pre><code class=\"language-js\">export default {\n    methods: {\n        handleClick() {\n            \/\/ \u901a\u77e5\u7236\u7ec4\u4ef6\n            \/\/ this.$emit(&quot;\u4e8b\u4ef6\u540d&quot;, ...\u53c2\u6570)\n            this.$emit(&quot;active&quot;) \n        }\n    }\n};\n<\/code><\/pre>\n<p>\u7236\u7ec4\u4ef6\u63a5\u6536\u4e8b\u4ef6<\/p>\n<pre><code class=\"language-html\">&lt;item :isActive=&quot;curActive === 'A'&quot; @active=&quot;curActive = 'A'&quot;&gt; A &lt;\/item&gt;\n<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u662f\u51fd\u6570\uff0c\u51fd\u6570\u7684\u53c2\u6570\u4e3a\u5b50\u7ec4\u4ef6\u4f20\u9012\u7684\u53c2\u6570\u3002<\/p>\n<h3>\u5f00\u53d1\u73af\u5883\u89e3\u51b3\u8de8\u57df\u95ee\u9898<\/h3>\n<p>\u5728vue.config.js\u4e2d\u6dfb\u52a0\u5185\u5bb9\uff1a<\/p>\n<pre><code class=\"language-js\">\/\/ vue-cli\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u5927\u90e8\u5206\u90fd\u662fwebpack\u7684\u914d\u7f6e\n\nconst { defineConfig } = require(&quot;@vue\/cli-service&quot;);\n\n\/\/ commonjs\u89c4\u8303\n\/\/ webpack\u5728\u52a0\u8f7d\u914d\u7f6e\u65f6\u662fnode.js\u73af\u5883\uff0cnode.js\u73af\u5883\u9ed8\u8ba4\u652f\u6301commonjs\u89c4\u8303\n\/\/ webpack\u5728\u8fd0\u884c\u6253\u5305\u7684\u65f6\u5019\u662fwebpack\u73af\u5883\uff0cwebpack\u73af\u5883\u9ed8\u8ba4\u90fd\u652f\u6301\nmodule.exports = defineConfig({\n  transpileDependencies: true,\n  devServer: {\n    proxy: {\n      \/\/ \u914d\u7f6e\u4ee3\u7406\n      &quot;\/x&quot;: { \/\/ \u4ee5\/x\u5f00\u5934\u7684\u8bf7\u6c42\u90fd\u4f1a\u88ab\u4ee3\u7406\n        target: &quot;https:\/\/api.bilibili.com&quot;,\n        changeOrigin: true,\n        onProxyReq(proxyReq) {\n          \/\/ \u6dfb\u52a0\u8bf7\u6c42\u5934\n          proxyReq.setHeader(&quot;origin&quot;, &quot;https:\/\/www.bilibili.com&quot;);\n          proxyReq.setHeader(&quot;referer&quot;, &quot;https:\/\/www.bilibili.com\/v\/channel&quot;);\n        }\n      }\n    },\n  },\n});\n<\/code><\/pre>\n<h3>\u7ed9\u5b50\u7ec4\u4ef6\u8bbe\u7f6e\u6837\u5f0f<\/h3>\n<p>\u901a\u5e38\u6211\u4eec\u5e0c\u671b\u7ec4\u4ef6\u72ec\u7acb\uff0c\u800c\u4e0d\u53d7\u5176\u4ed6\u5f71\u54cd\uff0c\u56e0\u6b64\u4e0d\u5efa\u8bae\u76f4\u63a5\u7ed9\u5b50\u7ec4\u4ef6\u8bbe\u7f6e\u6837\u5f0f\uff0c\u53ef\u901a\u8fc7\u5c5e\u6027\u4f20\u9012\u65b9\u5f0f\uff0c\u5c06\u6837\u5f0f\u4f20\u9012\u7ed9\u5b50\u7ec4\u4ef6\u3002<\/p>\n<pre><code class=\"language-html\">&lt;template&gt;\n    &lt;i class=&quot;iconfont&quot; :class=&quot;[iconClass, extraClass]&quot;&gt;&lt;\/i&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\nlet iconMap = {\n    ...\n};\n\nexport default {\n    props: {\n        type: {\n            type: String,\n            required: true,\n        },\n        extraClass: {\n            type: String,\n            default: &quot;&quot;,\n        }\n    },\n    computed: {\n        iconClass() {\n            return iconMap[this.type];\n        }\n    }\n};\n&lt;\/script&gt;\n\n&lt;style scoped&gt;\n@import &quot;\/\/at.alicdn.com\/t\/c\/font_4633489_dx291zwcocm.css&quot;;\n&lt;\/style&gt;\n<\/code><\/pre>\n<p>\u4f7f\u7528<\/p>\n<pre><code class=\"language-html\">&lt;icon :type=&quot;'search'&quot; extraClass=&quot;red&quot;&gt;&lt;\/icon&gt;\n&lt;style scoped&gt;\n.red {\n    color: red;\n}\n&lt;\/style&gt;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u811a\u624b\u67b6\u642d\u5efa\u5de5\u7a0b vue-cli vue-cli\u96c6\u6210\u4e86\u8bb8\u591a\u524d\u7aef\u6280\u672f\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a webpack babe [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-792","post","type-post","status-publish","format-standard","hentry","category-front-end"],"_links":{"self":[{"href":"https:\/\/levin.ink\/index.php?rest_route=\/wp\/v2\/posts\/792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/levin.ink\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/levin.ink\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/levin.ink\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/levin.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=792"}],"version-history":[{"count":2,"href":"https:\/\/levin.ink\/index.php?rest_route=\/wp\/v2\/posts\/792\/revisions"}],"predecessor-version":[{"id":846,"href":"https:\/\/levin.ink\/index.php?rest_route=\/wp\/v2\/posts\/792\/revisions\/846"}],"wp:attachment":[{"href":"https:\/\/levin.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/levin.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/levin.ink\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}