{"id":408,"date":"2022-04-19T16:09:32","date_gmt":"2022-04-19T08:09:32","guid":{"rendered":"http:\/\/gjweb.top\/?p=408"},"modified":"2022-04-19T16:09:33","modified_gmt":"2022-04-19T08:09:33","slug":"09-router%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=408","title":{"rendered":"09. router\u7684\u4f7f\u7528"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>&lt;script&gt;<br> &nbsp; &nbsp;import { useRouter } from 'vue-router'<br> &nbsp; &nbsp;exprot default {<br> &nbsp;      setup(){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;const router = useRouter();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;const click = ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;router.push({name:\"routerName\"})<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;click<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<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=\"router\u52a8\u6001\u4f20\u53c2\">router\u52a8\u6001\u4f20\u53c2<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*router.js*\/<br>{<br> &nbsp; &nbsp;path:'\/shop\/id',<br> &nbsp; &nbsp;name:'shop',<br> &nbsp; &nbsp;component: () =&gt; import(componentsName)<br>}<br>\u200b<br>\/***************************************************\/<br>\/*<br> * id \u4f5c\u4e3a\u53c2\u6570<br> * shop\/1<br>*\/<br>\/*shop.js*\/<br>&lt;script&gt;<br> &nbsp; &nbsp;import { useRoute } from 'vue-router'<br>    export default {<br> &nbsp; &nbsp; &nbsp; &nbsp;name:\"shop\",<br> &nbsp; &nbsp; &nbsp; &nbsp;setup(){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;const route = useRoute() <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(route.params) \/\/ \u8def\u7531\u4f20\u9012\u7684\u53c2\u6570<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<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=\"composition-api-\u8df3\u8f6c\">Composition API \u8df3\u8f6c<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;script setup&gt;<br> &nbsp; &nbsp;import { useRouter } from \"vue-router\"<br>    let router = useRouter();<br>    const goHome = ()=&gt;{<br> &nbsp; &nbsp; &nbsp; router.push(\"\/xxx\")<br> &nbsp;  }<br>&lt;\/script&gt;<br>&lt;template&gt;<br>    &lt;button @click=\"goHome\"&gt;\u8df3\u8f6c&lt;\/button&gt;<br>&lt;\/template&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>router\u52a8\u6001\u4f20\u53c2 Composition API \u8df3\u8f6c &lt;script setup&gt; &#038;nbs [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":["post-408","post","type-post","status-publish","format-standard","hentry","category-composition"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/408","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=408"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/408\/revisions"}],"predecessor-version":[{"id":409,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/408\/revisions\/409"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}