为什么vue-router只切换URL而不切换视图组件?

huangapple go评论58阅读模式
英文:

Why vue-router is toggling only the url but not the view component

问题

While clicking the nav link, Vue Router is toggling only the URL but not the view component, even though there don't seem to be any errors. The console shows that only the Home component is in the view, but not the Not Main component after clicking the second nav link.

英文:

While clicking the nav link vue-router is toggling only the url but not the view component, even though I don't seem to have any error. The console shows that only the Home component is at the view, but hot the Not main component after clciking the second nav link.

https://stackblitz.com/edit/vue-fsg2l2?file=src%2Fcomponents%2FHeader.vue,src%2Frouter%2Findex.js,src%2Fviews%2FHome.vue,src%2Fviews%2FShops.vue,src%2Fcomponents%2FMain.vue

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// router index.js

import VueRouter from &#39;vue-router&#39;;
import Home from &#39;../views/Home.vue&#39;;
import Shops from &#39;../views/Shops.vue&#39;;
const router = VueRouter.createRouter({
  history: VueRouter.createWebHashHistory(),
  routes: [
    {
      path: &#39;/&#39;,
      name: &#39;Home&#39;,
      component: Home,
    },
    {
      path: &#39;/shops&#39;,
      name: &#39;Shops&#39;,
      component: Shops,
    },
  ],
});

export default router;


// Home view

&lt;template&gt;
  &lt;div class=&quot;wrapper&quot;&gt;
    &lt;Header /&gt;
    &lt;Main /&gt;
    &lt;Footer /&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import Header from &#39;../components/Header.vue&#39;;
import Footer from &#39;../components/Footer.vue&#39;;
import Main from &#39;../components/Main.vue&#39;;

export default {
  name: &#39;Home&#39;,
  components: {
    Header,
    Footer,
    Main,
  },
};
&lt;/script&gt;

&lt;style lang=&quot;scss&quot; scoped&gt;&lt;/style&gt;


// anoteher view

&lt;template&gt;
  &lt;div class=&quot;wrapper&quot;&gt;
    &lt;NotMain /&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import NotMain from &#39;../components/NotMain.vue&#39;;

export default {
  name: &#39;Shops&#39;,
  components: {
    NotMain,
  },
};
&lt;/script&gt;

&lt;style lang=&quot;scss&quot; scoped&gt;&lt;/style&gt;

<!-- end snippet -->

答案1

得分: 0

<router-view></router-view> 在 app.js 中没有指定。这解决了问题。我需要将 <router-view></router-view> 放在 app.js 的主组件中。

英文:

<router-view></router-view> not specified at app.js That made the trick. I needed to post the <router-view></router-view> inside the app.js main component

答案2

得分: 0

在你的 app.vue 文件中,只有模板中的 router-view 部分会在路由更改时发生变化,其他内容将在每个路由上都显示。根据在 stackBlitz 上的检查,你的路由已经按预期工作,并且视图更改已经反映出来。

<template>
  <div id="app">
    <Header />
    <router-view></router-view>
    <Footer />
  </div>
</template>
英文:

In your app.vue only router-view part of template will change on change of route, rest all will display on every route. As checked on the stackBlitz your routes are working as expected and view changes are reflecting already.

&lt;template&gt;
  &lt;div id=&quot;app&quot;&gt;
    &lt;Header /&gt;
    &lt;router-view&gt;&lt;/router-view&gt;
    &lt;Footer /&gt;
  &lt;/div&gt;
&lt;/template&gt;

huangapple
  • 本文由 发表于 2023年5月21日 02:49:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定