即使移动到其他组件后,Router View 仍会继续显示组件信息

Posted

技术标签:

【中文标题】即使移动到其他组件后,Router View 仍会继续显示组件信息【英文标题】:Router View keep displaying the component information even after moving onto other component 【发布时间】:2021-10-11 08:00:21 【问题描述】:

我正在通过创建一个练习 APP 来学习 VueJS,但我被困在 Authors 组件内部我有一个作者列表的地方。我希望能够单击列表项并导航到AuthorDetail 组件,到目前为止一切正常。当我使用顶部的导航栏移动到 HomeAbout 等其他视图时,就会出现问题,AuthorDetail 组件保持可见(它应该消失!)。

App.vue内的代码

<template>
  <div id="nav">
    <router-link to="/">Home</router-link> |
    <router-link to="/authors">Authors</router-link> | 
    <router-link to="/about">About</router-link>
  </div>
  <router-view />
</template>

<style lang="scss">
#app 
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  max-width: 480px;
  margin: auto;
  border: 1px solid #2c3e50;
  border-radius: 5px;
  padding: 20px;


#nav 
  padding: 10px;
  border-bottom: 1px solid #e2e2e2;
  margin-bottom: 30px;

  a 
    font-weight: bold;
    color: #2c3e50;

    &.router-link-exact-active 
      color: #42b983;
    
  

</style>

router/index.js中的代码

import  createRouter, createWebHistory  from "vue-router";
import Home from "../views/Home.vue";
import Authors from "../views/Authors.vue";
import AuthorDetail from "../views/AuthorDetail.vue";

const routes = [
  
    path: "/",
    name: "Home",
    component: Home,
  ,
  
    path: "/authors",
    name: 'Authors',
    component: Authors
  ,
  
    path: "/authors/:id",
    name: "AuthorDetail",
    component: AuthorDetail
  ,
  
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue"),
  ,
];

const router = createRouter(
  history: createWebHistory(process.env.BASE_URL),
  routes,
);

export default router;

Authors 组件内的代码。

<template>
    <div>
        <h1>Authors</h1>
        <p>Most Popular Authors (TheTestRequest API)</p>

        <div class="authors-list" :key="author.id" v-for="author in authors">
            <router-link :to=" name: 'AuthorDetail', params:  id: author.id">
                <AuthorCard @click="showAuthor(author.id)" :author="author"></AuthorCard>
            </router-link>
        </div>
    </div>
</template>

<script>
    import AuthorCard from '@/components/AuthorCard'

    export default 
        name: "Authors",
        components: 
            AuthorCard
        ,
        data()
            return 
                authors: []
            
        ,
        methods: 
            async fetchAuthors()
                const res = await fetch('https://thetestrequest.com/authors')
                const data = await res.json()
                return data
            ,
            showAuthor(authorId)
                console.log("Author Clicked", authorId);
            
        ,
        async created() 
            this.authors = await this.fetchAuthors()
        ,
    
</script>

<style lang="scss" scoped>
    .authors-list 
        margin-top: 2em;
        // transition: box-shadow .3s;

        a 
            text-decoration: none;
            color: black;
        
    
</style>

注意:我正在使用thetestrequest.com 来获取此练习应用的数据。

UI 示例:

【问题讨论】:

【参考方案1】:

原来控制台中有一个错误帮助我解决了问题。

错误:

AuthorDetail.vue?0959:6 Uncaught (in promise) TypeError: Cannot read property 'avatar' of null
    at Proxy.eval (AuthorDetail.vue?0959:6)
    at renderComponentRoot (runtime-core.esm-bundler.js?5c40:1168)
    at componentEffect (runtime-core.esm-bundler.js?5c40:5214)
    at reactiveEffect (reactivity.esm-bundler.js?a1e9:42)
    at effect (reactivity.esm-bundler.js?a1e9:17)
    at setupRenderEffect (runtime-core.esm-bundler.js?5c40:5167)
    at mountComponent (runtime-core.esm-bundler.js?5c40:5126)
    at processComponent (runtime-core.esm-bundler.js?5c40:5084)
    at patch (runtime-core.esm-bundler.js?5c40:4690)
    at componentEffect (runtime-core.esm-bundler.js?5c40:5287)

所以在AuthorDetail 模板中进行简单的检查v-if="avatar" 对我有用。

【讨论】:

以上是关于即使移动到其他组件后,Router View 仍会继续显示组件信息的主要内容,如果未能解决你的问题,请参考以下文章

SignalR 在某些限制后未处理请求。即使在相同或不同机器上的其他浏览器中,选项卡仍会加载

即使将其可见性设置为 GONE,Android View 仍会获得 OnClick()

使用 react-router 链接到其他组件

vue-router应用到组件中时报错Unknown custom element: <router-view>

即使在指针设置为 nil 后 UIMenuItem 仍会继续显示

转 router-view 的理解