{"id":1046,"date":"2023-03-12T10:03:17","date_gmt":"2023-03-12T02:03:17","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1046"},"modified":"2023-04-29T12:40:59","modified_gmt":"2023-04-29T04:40:59","slug":"vue-refresh-current-page-or-component","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/vue-refresh-current-page-or-component\/","title":{"rendered":"Vue\u5237\u65b0\u5f53\u524d\u9875\u9762\u6216\u7ec4\u4ef6"},"content":{"rendered":"<p>\u5229\u7528<code>v-if<\/code>\u63a7\u5236<code>router-view<\/code>\uff0c\u5728\u6839\u7ec4\u4ef6APP.vue\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u5237\u65b0\u65b9\u6cd5\uff0c\u7136\u540e\u901a\u8fc7<code>provide\/inject<\/code>\u65b9\u5f0f\u8c03\u7528\u3002<\/p>\n<p>1\u3001\u4fee\u6539APP.vue\uff0c\u901a\u8fc7\u58f0\u660e<code>reload<\/code>\u65b9\u6cd5\uff0c\u63a7\u5236<code>router-view<\/code>\u7684\u663e\u793a\u6216\u9690\u85cf\uff0c\u4ece\u800c\u63a7\u5236\u9875\u9762\u7684\u518d\u6b21\u52a0\u8f7d\uff0c\u8fd9\u8fb9\u5b9a\u4e49\u4e86<code>isRouterAlive<\/code>\u6765\u63a7\u5236<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-javascript\">&lt;template&gt;\n  &lt;div id=&quot;app&quot;&gt;\n    &lt;router-view v-if=&quot;isRouterAlive&quot;\/&gt;\n  &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\nexport default {\n  name: &#039;App&#039;,\n  provide () {\n    return {\n      reload: this.reload\n    }\n  },\n  data () {\n    return {\n      isRouterAlive: true\n    }\n  },\n  methods: {\n    reload () {\n      this.isRouterAlive = false\n      this.$nextTick(function () {\n        this.isRouterAlive = true\n      })\n    }\n  }\n}\n&lt;\/script&gt;<\/code><\/pre>\n<p>2\u3001\u5728\u9700\u8981\u5237\u65b0\u7684\u9875\u9762\u4e2d\u6ce8\u5165<code>(inject)<\/code>\u5728App.vue\u7ec4\u4ef6\u63d0\u4f9b<code>(provide)<\/code>\u7684<code>reload<\/code>\u4f9d\u8d56\uff0c\u7136\u540e\u76f4\u63a5\u4f7f\u7528<code>this.reload()<\/code>\u8c03\u7528\u5373\u53ef<\/p>\n<pre><code class=\"language-javascript\">export default {\n  inject: [&#039;reload&#039;], \/\/ \u6ce8\u5165\u4f9d\u8d56\n  name: &#039;country&#039;,\n  data () {\n    countryCode: &#039;&#039;,\n    countryList: []\n  },\n  methods: {\n    changeCountry () {\n      switch (this.countryCode) {\n        case &#039;other&#039;:\n          $(&#039;#country_name&#039;)[0].disabled = false\n          break\n        default:\n          $(&#039;#country_name&#039;)[0].disabled = true\n          this.reload() \/\/ \u5237\u65b0\u9875\u9762\n          break\n      }\n    }\n  }\n}\n&lt;\/script&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5229\u7528v-if\u63a7\u5236router-view\uff0c\u5728\u6839\u7ec4\u4ef6APP.vue\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u5237\u65b0\u65b9\u6cd5\uff0c\u7136\u540e\u901a\u8fc7provide\/in [&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-1046","post","type-post","status-publish","format-standard","hentry","category-vue"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1046","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=1046"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1046\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}