{"id":718,"date":"2023-06-04T17:53:19","date_gmt":"2023-06-04T09:53:19","guid":{"rendered":"http:\/\/gjweb.top\/?p=718"},"modified":"2023-06-04T17:53:20","modified_gmt":"2023-06-04T09:53:20","slug":"12-%e7%bb%84%e4%bb%b6%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=718","title":{"rendered":"12.\u7ec4\u4ef6\u8be6\u89e3"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"\u5168\u5c40\u7ec4\u4ef6\">\u5168\u5c40\u7ec4\u4ef6<\/h2>\n\n\n\n<p>\u521b\u5efa\u7ec4\u4ef6 GlobalComponent.vue<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;<br>    &lt;h1&gt;this is globalComponent&lt;\/h1&gt;<br>&lt;\/template&gt;<\/code><\/pre>\n\n\n\n<p>\u5728 main.ts \u4e2d\u5f15\u5165\u7ec4\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { createApp } from 'vue'<br>import '.\/style.css'<br>import App from '.\/App.vue'<br>import GlobalComponent from \".\/component\/glocalComponent\"<br>const app = createApp(App)<br>app.component(\"GlobalComponent\",GlobalComponent)<br>app.mount(\"#app\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u9012\u5f52\u7ec4\u4ef6\">\u9012\u5f52\u7ec4\u4ef6<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u5b50\u7ec4\u4ef6-helloworld-vue\">\u5b50\u7ec4\u4ef6 Helloworld.vue<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;<br> &nbsp;&lt;ul v-for=\"(item,index) in data\" :key=\"index\"&gt;<br> &nbsp; &nbsp;&lt;li&gt;{{ item.name }}&lt;\/li&gt;<br> &nbsp; &nbsp;&lt;HelloWorld v-if=\"item.children?.length!=0\" :data=\"item.children\"&gt;&lt;\/HelloWorld&gt;<br> &nbsp;&lt;\/ul&gt;<br>&lt;\/template&gt;<br>&lt;script setup lang=\"ts\"&gt;<br>type TreeList = {<br> &nbsp;name: string;<br> &nbsp;children?: TreeList&#91;] | &#91;];<br>}<br>type Props&lt;T&gt; = {<br> &nbsp;data?: T&#91;] | &#91;];<br>};<br>defineProps&lt;Props&lt;TreeList&gt;&gt;()<br>&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u7236\u7ec4\u4ef6\u8c03\u7528\">\u7236\u7ec4\u4ef6\u8c03\u7528<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;<br> &nbsp;&lt;div&gt; this is app&lt;\/div&gt;<br> &nbsp;&lt;HelloWorld :data=\"listData\"&gt;&lt;\/HelloWorld&gt;<br>&lt;\/template&gt;<br>&lt;script setup lang=\"ts\"&gt;<br>import HelloWorld from \".\/components\/HelloWorld.vue\";<br>const listData = &#91;<br>  {<br> &nbsp; &nbsp;name:\"1\",<br> &nbsp; &nbsp;children:&#91;<br> &nbsp; &nbsp;  {name:'1-1'},<br> &nbsp; &nbsp;  {name:'1-2'},<br> &nbsp; &nbsp;  {name:'1-3'},<br> &nbsp; &nbsp;  {name:'1-4'},<br> &nbsp;  ]<br>  },<br>  {<br> &nbsp; &nbsp;name:'2',<br> &nbsp; &nbsp;children:&#91;<br> &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;name:\"2-1\",<br> &nbsp; &nbsp; &nbsp; &nbsp;children:&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp;  {name:'2-1-1'}<br> &nbsp; &nbsp; &nbsp;  ]<br> &nbsp; &nbsp;  },<br> &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;name:\"2-2\"<br> &nbsp; &nbsp;  }<br> &nbsp;  ]<br>  },<br>  {<br> &nbsp; &nbsp;name:\"3\"<br>  }<br>]<br>&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u6253\u5370\u6548\u679c\">\u6253\u5370\u6548\u679c<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/raw.githubusercontent.com\/gongjianOnline\/ImgHosting\/main\/img\/1683984780113.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/raw.githubusercontent.com\/gongjianOnline\/ImgHosting\/main\/img\/1683984780113.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\"\/><\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u914d\u7f6e\u9012\u5f52\u7ec4\u4ef6\u8d77\u522b\u540d\">\u914d\u7f6e\u9012\u5f52\u7ec4\u4ef6\u8d77\u522b\u540d<\/h3>\n\n\n\n<p>\u5728\u539f\u5148\u7684\u57fa\u7840\u4e0a\u65b0\u521b\u5efa script<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;<br>export default {<br> &nbsp; &nbsp;name:'xxx' \/\/ \u5728\u9012\u5f52\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528 xxx<br>}<br>&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u52a8\u6001\u7ec4\u4ef6\">\u52a8\u6001\u7ec4\u4ef6<\/h2>\n\n\n\n<p>\u4f7f\u7528 component \u6807\u7b7e\u901a\u8fc7\u5c5e\u6027 is \u52a8\u6001\u6e32\u67d3\u7ec4\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;<br> &nbsp;&lt;div&gt; this is app&lt;\/div&gt;<br> &nbsp;&lt;button @click=\"handleClick\"&gt;\u5207\u6362&lt;\/button&gt;<br> &nbsp;&lt;component :is=\"xxx\"&gt;&lt;\/component&gt;<br>&lt;\/template&gt;<br>&lt;script setup lang=\"ts\"&gt;<br>import ComA from \".\/components\/comA.vue\"<br>import ComB from \".\/components\/comB.vue\"<br>import {reactive,ref,markRaw} from \"vue\"<br>\u200b<br>const ComponentName = reactive({<br> &nbsp;\"ComA\":markRaw(ComA),<br> &nbsp;\"ComB\":markRaw(ComB),<br>});<br>const xxx = ref(ComponentName&#91;'ComA'])<br>const handleClick = ()=&gt;{<br> &nbsp;xxx.value = ComponentName&#91;\"ComB\"]<br>}<br>&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u52a8\u6001\u6e32\u67d3\u7684\u7ec4\u4ef6\u5b9e\u4f8b\u4f1a\u88ab proxy \u52ab\u6301, \u9020\u6210\u4e0d\u5fc5\u8981\u7684\u6027\u80fd\u6d6a\u8d39, \u53ef\u4f7f\u7528 markRaw(Component) \u5305\u88f9\u7ec4\u4ef6, \u4f1a\u6dfb\u52a0 &#8220;__ skip __&#8221;\u5c5e\u6027, vue3 \u9047\u5230\u6539\u5c5e\u6027\u4f1a\u8df3\u8fc7\u52ab\u6301<\/p><\/blockquote>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u5f02\u6b65\u7ec4\u4ef6\">\u5f02\u6b65\u7ec4\u4ef6<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>suspense \u5728\u5b98\u7f51\u8fd8\u672a\u6b63\u5f0f\u53d1\u5e03<\/p><\/blockquote>\n\n\n\n<p>suspense \u7ec4\u4ef6,\u4f7f\u7528\u573a\u666f\u53ef\u7528\u4e8e\u9aa8\u67b6\u5c4f\u6548\u679c<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u5b50\u7ec4\u4ef6\u58f0\u660e\">\u5b50\u7ec4\u4ef6\u58f0\u660e<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;<br> &nbsp;&lt;h1&gt;this is Component {{ data }}&lt;\/h1&gt;<br>&lt;\/template&gt;<br>&lt;script setup lang=\"ts\"&gt; &nbsp;<br>import {ref} from \"vue\"<br>const response = ()=&gt;{<br> &nbsp;return new Promise((resolve)=&gt;{<br> &nbsp; &nbsp;setTimeout(()=&gt;{<br> &nbsp; &nbsp; &nbsp;resolve(\"helloWorld Component\")<br> &nbsp;  },5000)<br>  })<br>}<br>const data = ref&lt;string&gt;(\"\");<br>const res = await response();<br>data.value = res as string;<br>&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u7236\u7ec4\u4ef6\u4f7f\u7528\">\u7236\u7ec4\u4ef6\u4f7f\u7528<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;<br> &nbsp;&lt;HelloWorld&gt;&lt;\/HelloWorld&gt;<br> &nbsp;&lt;suspense&gt;<br> &nbsp; &nbsp;&lt;template #default&gt;<br> &nbsp; &nbsp; &nbsp;&lt;AsyncComponent&gt;&lt;\/AsyncComponent&gt;<br> &nbsp; &nbsp;&lt;\/template&gt;<br> &nbsp; &nbsp;&lt;template #fallback&gt;<br> &nbsp; &nbsp; &nbsp;&lt;div&gt;loging...&lt;\/div&gt;<br> &nbsp; &nbsp;&lt;\/template&gt;<br> &nbsp;&lt;\/suspense&gt;<br>&lt;\/template&gt;<br>&lt;script setup lang=\"ts\"&gt;<br>import {defineAsyncComponent} from \"vue\"<br>import ComA from \".\/components\/comA.vue\"<br>import ComB from \".\/components\/comB.vue\"<br>const AsyncComponent = defineAsyncComponent(()=&gt;import(\".\/components\/HelloWorld.vue\"))<br>&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u5f02\u6b65\u7ec4\u4ef6\u7684\u5b8c\u6574\u5199\u6cd5\">\u5f02\u6b65\u7ec4\u4ef6\u7684\u5b8c\u6574\u5199\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const AsyncComponent = defineAsyncComponent({<br>  loader:()=&gt;import(\".\/components\/HelloWorld.vue\") , \/\/ \u5f02\u6b65\u52a0\u8f7d\u51fd\u6570<br>  loadingComponent:ComA, \/\/ \u52a0\u8f7d\u65f6\u4f7f\u7528\u7684\u7ec4\u4ef6<br>  delay:200, \/\/ \u5c55\u793a\u52a0\u8f7d\u7ec4\u4ef6\u524d\u7684\u5ef6\u8fdf, \u9ed8\u8ba4200\u6beb\u79d2<br>  errorComponent:ComB,\/\/ \u52a0\u8f7d\u5931\u8d25\u540e\u7684\u5c55\u793a\u7ec4\u4ef6 <br>  timeout:1 \/\/ \u8bbe\u5b9a\u8d85\u65f6\u65f6\u95f4,\u52a0\u8f7d\u8d85\u65f6\u4f1a\u5c55\u793a\u5931\u8d25\u7ec4\u4ef6<br>})<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5168\u5c40\u7ec4\u4ef6 \u521b\u5efa\u7ec4\u4ef6 GlobalComponent.vue \u5728 main.ts \u4e2d\u5f15\u5165\u7ec4\u4ef6 \u9012\u5f52\u7ec4\u4ef6 \u5b50\u7ec4\u4ef6  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"class_list":["post-718","post","type-post","status-publish","format-standard","hentry","category-vue3"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=718"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/718\/revisions"}],"predecessor-version":[{"id":719,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/718\/revisions\/719"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}