{"id":10006083,"date":"2022-08-02T13:51:50","date_gmt":"2022-08-02T05:51:50","guid":{"rendered":"https:\/\/ityet.com\/?p=10006083"},"modified":"2022-08-02T13:51:50","modified_gmt":"2022-08-02T05:51:50","slug":"%e6%9d%be%e5%93%a5%e6%89%8b%e6%8a%8a%e6%89%8b%e6%95%99%e4%bd%a0%e5%9c%a8-vue3-%e4%b8%ad%e8%87%aa%e5%ae%9a%e4%b9%89%e6%8f%92%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006083","title":{"rendered":"\u677e\u54e5\u624b\u628a\u624b\u6559\u4f60\u5728 Vue3 \u4e2d\u81ea\u5b9a\u4e49\u63d2\u4ef6"},"content":{"rendered":"<p>@[toc]<br \/>\u6700\u8fd1\u5728\u5f55 TienChin \u9879\u76ee\uff0c\u9879\u76ee\u6d89\u53ca\u5230\u4e86 Vue \u4e2d\u63d2\u4ef6\u7684\u5b9a\u4e49\uff0c\u56e0\u6b64\u6574\u4e86\u8fd9\u4e48\u4e00\u7bc7\u6587\u7ae0\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5728 Vue3 \u4e2d\u5b9a\u4e49\u63d2\u4ef6\uff0c\u8fd9\u4e2a\u6280\u80fd\u638c\u63e1\u4e86\uff0c\u5c31\u53ef\u4ee5\u770b\u61c2 TienChin \u524d\u7aef\u4ee3\u7801\u4e86\u3002<\/p>\n<h2>1. Vue \u63d2\u4ef6<\/h2>\n<p>\u5728 Vue \u4e2d\uff0c\u4e00\u4e9b\u7b80\u5355\u7684\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5b9a\u4e49\u4e3a\u5168\u5c40\u65b9\u6cd5\uff0c\u7136\u540e\u6302\u5230 Vue \u4e0a\u5c31\u80fd\u4f7f\u7528\u4e86\uff0c\u4f8b\u5982\u5728 vhr \u4e2d\uff0c\u6211\u4eec\u5c06\u7f51\u7edc\u8bf7\u6c42\u65b9\u6cd5\u8fdb\u884c\u5c01\u88c5\uff0c\u7136\u540e\u6302\u5230\u4e86 Vue.prototype \u4e0a\u5c31\u53ef\u4ee5\u4e86\uff0c\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre><code class=\"js\">import {postRequest} from &quot;.\/utils\/api&quot;;\n\nVue.prototype.postRequest = postRequest;<\/code><\/pre>\n<p>\u7136\u540e\u5728\u4f7f\u7528\u7684\u5730\u65b9\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7 this.postRequest \u53bb\u4f7f\u7528\u4e86\u3002<\/p>\n<blockquote><p>\u5c0f\u4f19\u4f34\u4eec\u9700\u8981\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5728 Vue3 \u4e2d\u6709\u6240\u53d8\u5316\uff0cprototype \u53d8\u4e3a\u4e86 config.globalProperties\uff0c\u4e5f\u5c31\u662f\u5728 Vue3 \u4e2d\u518d\u60f3\u8981\u6302\u8f7d\u5168\u5c40\u65b9\u6cd5\uff0c\u5e94\u8be5\u662f <code>const app = createApp(App);app.config.globalProperties.useDict = useDict<\/code> \u8fd9\u79cd\u5f62\u5f0f\u4e86\uff08\u5177\u4f53\u6211\u5c06\u5728 TienChin \u9879\u76ee\u4e2d\u548c\u5927\u5bb6\u7ec6\u804a\uff09\u3002<\/p><\/blockquote>\n<p>\u8fd9\u4e5f\u7b97\u662f\u4e00\u79cd\u63d2\u4ef6\u5b9a\u4e49\u65b9\u5f0f\uff0c\u4f46\u662f\u8fd9\u79cd\u4e00\u822c\u9002\u7528\u4e8e\u4e00\u4e9b\u5de5\u5177\u65b9\u6cd5\uff0c\u65e0\u6cd5\u5b9a\u4e49\u4e00\u4e9b\u6bd4\u8f83\u590d\u6742\u7684\u63d2\u4ef6\uff0c\u590d\u6742\u7684\u63d2\u4ef6\u8fd8\u662f\u5f97\u901a\u8fc7 Vue \u4e2d\u63d0\u4f9b\u7684\u63d2\u4ef6\u5b9a\u4e49\u65b9\u5f0f\u6765\u5b9a\u4e49\u3002<\/p>\n<h2>2. \u81ea\u5b9a\u4e49\u63d2\u4ef6<\/h2>\n<h3>2.1 \u57fa\u672c\u7528\u6cd5<\/h3>\n<p>\u9996\u5148\u6211\u4eec\u65b0\u5efa\u4e00\u4e2a\u76ee\u5f55 plugins \u4e13\u95e8\u7528\u6765\u653e\u6211\u4eec\u7684\u63d2\u4ef6\uff0c\u7136\u540e\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e2d\u65b0\u5efa\u4e00\u4e2a index.js \u6587\u4ef6\u7528\u6765\u5f00\u53d1\u63d2\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"js\">export default {\n    install: (app, options) =&gt; {\n        console.log(&quot;\u6211\u7684\u7b2c\u4e00\u4e2a\u63d2\u4ef6&quot;)\n    }\n};<\/code><\/pre>\n<p>install \u4e2d\u7684\u65b9\u6cd5\u5c06\u4f1a\u88ab\u81ea\u52a8\u6267\u884c\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u53ef\u4ee5\u5728 main.js \u4e2d\u5f15\u5165\u6211\u4eec\u8fd9\u4e2a\u63d2\u4ef6\u4e86\uff1a<\/p>\n<pre><code class=\"js\">const app = createApp(App);\n\nimport plugin from '.\/plugins'\n\napp.use(plugin);<\/code><\/pre>\n<p>app.use \u5c31\u8868\u793a\u5f15\u5165\u63d2\u4ef6\uff0c\u5f15\u5165\u63d2\u4ef6\u4e4b\u540e\uff0c\u63d2\u4ef6\u4e2d\u7684 install \u65b9\u6cd5\u5c31\u4f1a\u88ab\u81ea\u52a8\u6267\u884c\u3002<\/p>\n<p>app.use \u65b9\u6cd5\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u5c31\u662f\u6211\u4eec\u5bfc\u5165\u7684\u63d2\u4ef6 js \u5bf9\u8c61\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u53ef\u9009\u7684\uff0c\u5927\u5bb6\u770b\u5230\u63d2\u4ef6\u5b9a\u4e49\u65f6\u5019\u7684 install \u65b9\u6cd5\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f Vue \u5b9e\u4f8b\uff0c\u8fd9\u662f\u81ea\u52a8\u4f20\u5165\u7684\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570 options \u5219\u662f\u6211\u4eec\u5728 app.use \u4e2d\uff0c\u901a\u8fc7\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u5165\u8fdb\u6765\u7684\u3002\u5f53\u7136\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u677e\u54e5\u6ca1\u6709\u4f20\u9012\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u5982\u6b64\u914d\u7f6e\u4e4b\u540e\uff0c\u63a5\u4e0b\u6765\u542f\u52a8\u9879\u76ee\uff0c\u63a7\u5236\u53f0\u5c31\u53ef\u4ee5\u770b\u5230\u6709\u65e5\u5fd7\u6253\u51fa\u4e86\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042256816\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8fd9\u6837\u7684\u63d2\u4ef6\u672a\u514d\u8fc7\u4e8e\u7b80\u5355\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u7ed9\u8fd9\u4e2a\u63d2\u4ef6\u52a0\u70b9\u6599\u3002<\/p>\n<h3>2.2 \u52a0\u5165\u7ec4\u4ef6<\/h3>\n<p>\u9996\u5148\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u7ec4\u4ef6\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"html\">&lt;template&gt;\n    &lt;div&gt;\n        &lt;h1&gt;&lt;a href=&quot;http:\/\/www.javaboy.org&quot;&gt;javaboy&lt;\/a&gt;&lt;\/h1&gt;\n    &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\n    export default {\n        name: &quot;MyBanner&quot;\n    }\n&lt;\/script&gt;<\/code><\/pre>\n<p>\u7136\u540e\u6211\u4eec\u73b0\u5728\u5c31\u53ef\u4ee5\u5728\u63d2\u4ef6\u4e2d\u5c06\u8fd9\u4e2a\u7ec4\u4ef6\u6ce8\u518c\u4e3a\u4e00\u4e2a\u5168\u5c40\u7ec4\u4ef6\u4e86\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"js\">import MyBanner from &quot;@\/plugins\/components\/MyBanner&quot;;\n\nexport default {\n    install: (app, options) =&gt; {\n        console.log(&quot;\u6211\u7684\u7b2c\u4e00\u4e2a\u63d2\u4ef6&quot;)\n        app.component('my-banner', MyBanner);\n    }\n};<\/code><\/pre>\n<p>\u9996\u5148\u5728\u63d2\u4ef6\u4e2d\u5bfc\u5165\u8fd9\u4e2a\u7ec4\u4ef6\uff0c\u7136\u540e\u901a\u8fc7 app \u8fdb\u884c\u7ec4\u4ef6\u6ce8\u518c\uff0c\u6ce8\u518c\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u9879\u76ee\u4efb\u610f\u4f4d\u7f6e\u4f7f\u7528 <code>my-banner<\/code> \u7ec4\u4ef6\u4e86\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"html\">&lt;template&gt;\n    &lt;div&gt;\n        &lt;my-banner&gt;&lt;\/my-banner&gt;\n    &lt;\/div&gt;\n&lt;\/template&gt;<\/code><\/pre>\n<p>\u6700\u7ec8\u663e\u793a\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042256817\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>2.3 \u52a0\u5165\u6307\u4ee4<\/h3>\n<p>\u6211\u4eec\u751a\u81f3\u8fd8\u53ef\u4ee5\u5728\u63d2\u4ef6\u4e2d\u6ce8\u518c\u4e00\u4e2a\u6307\u4ee4\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"js\">import MyBanner from &quot;@\/plugins\/components\/MyBanner&quot;;\n\nexport default {\n    install: (app, options) =&gt; {\n        console.log(&quot;\u6211\u7684\u7b2c\u4e00\u4e2a\u63d2\u4ef6&quot;)\n        app.component('my-banner', MyBanner);\n        app.directive(&quot;font-size&quot;, (el, binding, vnode) =&gt; {\n            var size = 16;\n            switch (binding.arg) {\n                case &quot;small&quot;:\n                    size = 16;\n                    break;\n                case &quot;large&quot;:\n                    size = 32;\n                    break;\n                default:\n                    size = 48;\n                    break;\n            }\n            el.style.fontSize = size + &quot;px&quot;;\n        });\n    }\n};<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u9879\u76ee\u4e2d\u968f\u65f6\u968f\u5730\u53bb\u4f7f\u7528\u8fd9\u4e2a\u6307\u4ee4\u4e86\uff0c\u4f8b\u5982\u5728\u6211\u4eec\u521a\u521a\u81ea\u5b9a\u4e49\u7684 my-banner \u4e2d\u4f7f\u7528\u8fd9\u4e2a\u6307\u4ee4\uff1a<\/p>\n<pre><code class=\"html\">&lt;template&gt;\n    &lt;div&gt;\n        &lt;h1&gt;&lt;a href=&quot;http:\/\/www.javaboy.org&quot; v-font-size:small&gt;javaboy&lt;\/a&gt;&lt;\/h1&gt;\n    &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\n    export default {\n        name: &quot;MyBanner&quot;\n    }\n&lt;\/script&gt;<\/code><\/pre>\n<p>\u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u901a\u8fc7 options \u5c06\u6307\u4ee4\u4e2d\u5b57\u4f53\u7684\u5927\u5c0f\u52a8\u6001\u7684\u4f20\u8fdb\u6765\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"js\">import MyBanner from &quot;@\/plugins\/components\/MyBanner&quot;;\n\nexport default {\n    install: (app, options) =&gt; {\n        console.log(&quot;\u6211\u7684\u7b2c\u4e00\u4e2a\u63d2\u4ef6&quot;)\n        app.component('my-banner', MyBanner);\n        app.directive(&quot;font-size&quot;, (el, binding, vnode) =&gt; {\n            var size = 16;\n            switch (binding.arg) {\n                case &quot;small&quot;:\n                    size = options.small;\n                    break;\n                case &quot;large&quot;:\n                    size = options.large;\n                    break;\n                default:\n                    size = options.defaut;\n                    break;\n            }\n            el.style.fontSize = size + &quot;px&quot;;\n        });\n    }\n};<\/code><\/pre>\n<p>options \u662f\u63d2\u4ef6\u6ce8\u518c\u65f6\u5019\u4f20\u5165\u7684\u4e00\u4e2a JSON \u53c2\u6570\uff0csmall\u3001large \u4ee5\u53ca default \u5206\u522b\u5bf9\u5e94\u7684\u5b57\u4f53\u591a\u5927\uff0c\u8981\u770b\u63d2\u4ef6\u6ce8\u518c\u65f6\u4f20\u5165\u7684\u503c\uff1a<\/p>\n<pre><code class=\"js\">const app = createApp(App);\n\nimport plugin from '.\/plugins'\n\napp.use(plugin, {small: 16, large: 32, default: 48});<\/code><\/pre>\n<p>\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5927\u5bb6\u770b\uff0c\u5c31\u662f options \u53c2\u6570\u7684\u503c\u3002<\/p>\n<blockquote><p>\u73b0\u5728\u5927\u5bb6\u60f3\u60f3\u6211\u4eec\u5e73\u65f6\u7528 ElementUI \u7684\u65f6\u5019\uff0cVue.use \u65b9\u6cd5\uff0c\u4f20\u5165 ElementUI\uff0c\u518d\u4f20\u5165\u4e00\u4e9b\u5176\u4ed6\u53c2\u6570\uff0c\u770b\u4e86\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff0cElementUI \u5f15\u5165\u5230\u5e95\u662f\u600e\u4e48\u4e2a\u5f15\u5165\u6cd5\u73b0\u5728\u5927\u5bb6\u5c31\u660e\u767d\u4e86\u5427\u3002<\/p><\/blockquote>\n<h3>2.4 provide &#038; inject<\/h3>\n<p>\u5728\u63d2\u4ef6\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 provide \u6765\u63d0\u4f9b\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5728\u9700\u8981\u4f7f\u7528\u8be5\u65b9\u6cd5\u7684\u5730\u65b9\uff0c\u901a\u8fc7 inject \u6ce8\u5165\u65b9\u6cd5\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"js\">import MyBanner from &quot;@\/plugins\/components\/MyBanner&quot;;\n\nexport default {\n    install: (app, options) =&gt; {\n        console.log(&quot;\u6211\u7684\u7b2c\u4e00\u4e2a\u63d2\u4ef6&quot;)\n        app.component('my-banner', MyBanner);\n        app.directive(&quot;font-size&quot;, (el, binding, vnode) =&gt; {\n            var size = 16;\n            switch (binding.arg) {\n                case &quot;small&quot;:\n                    size = options.small;\n                    break;\n                case &quot;large&quot;:\n                    size = options.large;\n                    break;\n                default:\n                    size = options.defaut;\n                    break;\n            }\n            el.style.fontSize = size + &quot;px&quot;;\n        });\n        const clickMe = () =&gt; {\n            console.log(&quot;==========clickMe=========&quot;)\n        }\n        app.provide('clickMe', clickMe);\n    }\n};<\/code><\/pre>\n<p>\u5728\u9700\u8981\u4f7f\u7528\u7684\u5730\u65b9\uff0c\u901a\u8fc7 inject \u6ce8\u5165\u65b9\u6cd5\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"html\">&lt;template&gt;\n    &lt;div&gt;\n        &lt;h1&gt;&lt;a href=&quot;http:\/\/www.javaboy.org&quot; v-font-size:small&gt;javaboy&lt;\/a&gt;&lt;\/h1&gt;\n    &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\n\n    import {inject} from &quot;vue&quot;;\n\n    export default {\n        name: &quot;MyBanner&quot;,\n        mounted() {\n            const clickMe = inject('clickMe');\n            clickMe();\n        }\n    }\n&lt;\/script&gt;<\/code><\/pre>\n<h2>3. \u5c0f\u7ed3<\/h2>\n<p>\u6574\u4f53\u4e0a\u6765\u8bf4\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u6765\u81ea\u5b9a\u4e49\u63d2\u4ef6\uff0c\u80fd\u591f\u5b9e\u73b0\u7684\u5185\u5bb9\u6bd4\u8f83\u4e30\u5bcc\u3002\u5982\u679c\u53ea\u662f\u60f3\u6302\u4e00\u4e2a\u5168\u5c40\u65b9\u6cd5\u6765\u7528\uff0c\u90a3\u4e48\u5176\u5b9e\u662f\u6ca1\u6709\u5fc5\u8981\u5b9a\u4e49\u63d2\u4ef6\u7684\u3002\u5982\u679c\u53ea\u662f\u60f3\u6302\u8f7d\u4e00\u4e2a\u5168\u5c40\u65b9\u6cd5\uff0c\u5728 Vue2 \u4e2d\u53ef\u4ee5\u6309\u7167\u5982\u4e0b\u65b9\u5f0f\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"js\">Vue.prototype.postRequest = postRequest;<\/code><\/pre>\n<p>\u5728 Vue3 \u4e2d\u5219\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"js\">app.config.globalProperties.useDict = useDict<\/code><\/pre>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042256814\">https:\/\/segmentfault.com\/a\/1190000042256814<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>@[toc]\u6700\u8fd1\u5728\u5f55 TienChin \u9879\u76ee\uff0c\u9879\u76ee\u6d89\u53ca\u5230\u4e86 Vue \u4e2d\u63d2\u4ef6\u7684\u5b9a\u4e49\uff0c\u56e0\u6b64\u6574\u4e86\u8fd9\u4e48\u4e00\u7bc7\u6587\u7ae0\uff0c\u624b\u628a\u624b [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006083","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006083"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006083\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}