{"id":1048,"date":"2023-03-12T10:05:27","date_gmt":"2023-03-12T02:05:27","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1048"},"modified":"2023-04-29T12:40:35","modified_gmt":"2023-04-29T04:40:35","slug":"vue-custom-component-simple-implementation-of-a-custom-component","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/vue-custom-component-simple-implementation-of-a-custom-component\/","title":{"rendered":"Vue\u81ea\u5b9a\u4e49\u7ec4\u4ef6(\u7b80\u5355\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7ec4\u4ef6)"},"content":{"rendered":"<p>\u5728\u7528Vue\u6784\u5efa\u9879\u76ee\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u6709\u65f6\u4f1a\u7528\u5230\u522b\u4eba\u5f00\u53d1\u7684\u7ec4\u4ef6\u5982<code>vue-router<\/code>\uff0c\u4f7f\u7528\u4ed6\u4eba\u7ec4\u4ef6\u7684\u6b63\u5e38\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p>1\u3001\u547d\u4ee4\u884c\u8fdb\u884c\u5b89\u88c5\uff0c\u6267\u884c<code>install<\/code><br \/>\n2\u3001\u5728Vue\u9879\u76ee\u4e2d\u7684\u5165\u53e3\u6587\u4ef6<code>main.js<\/code>\u4e2d\uff0c\u8fdb\u884c\u5bfc\u5165<br \/>\n3\u3001\u7136\u540e\u7528<code>Vue.use(plugin)<\/code>\u5f15\u5165\u8be5\u7ec4\u4ef6<\/p>\n<p><!-- more --><\/p>\n<p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u521b\u9020\u5c5e\u4e8e\u81ea\u5df1\u7684\u7ec4\u4ef6\uff0c\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p>1\u3001\u5728<code>components<\/code>\u6587\u4ef6\u4e2d\u521b\u5efatest\u6587\u4ef6<br \/>\n2\u3001\u5728test\u6587\u4ef6\u4e2d\uff0c\u521b\u5efa<code>index.js<\/code>\u548c<code>Test.vue<\/code><br \/>\n3\u3001<code>Test.vue<\/code>\u4e2d\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">&lt;template&gt;\n  &lt;div&gt;\n    &lt;div&gt;\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7ec4\u4ef6&lt;\/div&gt;\n  &lt;\/div&gt;\n&lt;\/template&gt;\n&lt;script&gt;\nexport default{\n  data () {\n    return {\n      msg: &#039;hello vue&#039;\n    }\n  },\n  components: {}\n}\n&lt;\/script&gt;\n&lt;style&gt;\n&lt;\/style&gt;<\/code><\/pre>\n<p>4\u3001test\u6587\u4ef6\u5939\u4e0b\u7684<code>index.js<\/code>\u4e2d\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">import MyTest from &#039;.\/Test.vue&#039;\nconst Test = {\n  install (Vue) {\n    Vue.component(&#039;Test&#039;, MyTest)\n  }\n}\nexport default Test<\/code><\/pre>\n<p>5\u3001\u5165\u53e3\u6587\u4ef6<code>main.js<\/code>\u8fdb\u884c\u76f8\u5173\u7684\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"language-javascript\">import Test from &#039;.\/components\/test&#039;\nVue.use(Test)<\/code><\/pre>\n<p>6\u3001\u5982\u6b64\u8fd9\u822c\uff0c\u5c31\u53ef\u4ee5\u5728\u5176\u5b83\u7ec4\u4ef6\u4e2d\u6b63\u5e38\u4f7f\u7528\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">&lt;template&gt;\n  &lt;div class=&quot;hello&quot;&gt;\n      &lt;Test&gt;&lt;\/Test&gt;\n  &lt;\/div&gt;\n&lt;\/template&gt;<\/code><\/pre>\n<p>\u81ea\u5b9a\u4e49\u7ec4\u4ef6Test\u7684\u5185\u5bb9(&quot;\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7ec4\u4ef6&quot;)\u5c06\u4f1a\u5c55\u793a\u51fa\u6765\u3002<\/p>\n<blockquote>\n<p>\u6ce8\uff1a<code>test<\/code>\u6587\u4ef6\u6307\u7684\u662f\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u6587\u4ef6\u5939\uff0c<code>index.js<\/code>\u6307\u7684\u662f\u7ec4\u4ef6\u7684\u5165\u53e3\u52a0\u8f7d\u6587\u4ef6\uff0c<code>Test.vue<\/code>\u6307\u7684\u662f\u7ec4\u4ef6\u6a21\u677f\u3002<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7528Vue\u6784\u5efa\u9879\u76ee\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u6709\u65f6\u4f1a\u7528\u5230\u522b\u4eba\u5f00\u53d1\u7684\u7ec4\u4ef6\u5982vue-router\uff0c\u4f7f\u7528\u4ed6\u4eba\u7ec4\u4ef6\u7684\u6b63\u5e38\u6b65\u9aa4\u5982\u4e0b\uff1a  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1048","post","type-post","status-publish","format-standard","hentry","category-vue"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1048"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1048\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}