路线更改时组件未加载

时间:2020-08-18 14:58:20

标签: vuejs2 vue-router

我想创建一个带有孩子路线的嵌套路线。

基本上我想要的是

https://localhost/contact渲染ContactList组件。

https://localhost/contact/add渲染ContactAdd组件。

我尝试过的是:

let Layout = {
  template: '<div>Layout Page <router-view></router-view></div>'
};
let Home = {
  template: '<div>this is the home page. Go to <router-link to="/contact">contact</router-link> </div>'
};

let ContactList = {
  template: '<div>this is contact list, click <router-link to="/contact/add">here</router-link> here to add contact</div>'
};

let ContactAdd = {
  template: '<div>Contact Add</div>'
}

let router = new VueRouter({
  routes: [{
    path: '/',
    redirect: 'home',
    component: Layout,
    children: [{
        path: 'home',
        component: Home
      },
      {
        path: 'contact',
        component: ContactList,
        children: [{
          path: 'add',
          component: ContactAdd
        }]
      },
    ]
  }]
});

new Vue({
  el: '#app',
  components: {
    'App': {
      template: '<div><router-view></router-view></div>'
    },
  },
  router
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vue-router@3.0.1/dist/vue-router.js"></script>
<section id="app">
  <app></app>
</section>

在这里,问题是当我将路线从/client更改为/client/add时,视图无法呈现。这里的嵌套儿童路线有什么问题?如何解决这个问题?

我查看了此文档https://router.vuejs.org/guide/essentials/nested-routes.html,但在这种情况下没有帮助。

1 个答案:

答案 0 :(得分:1)

您需要在<router-view>的模板中添加一个<ContactList>来加载其子路由。

或者,如果您想在ContactAdd中显示Layout,请将ContactAdd移动到path = /的子节点。

let Layout = {
  template: '<div>Layout Page <router-view></router-view></div>'
};
let Home = {
  template: '<div>this is the home page. Go to <router-link to="/contact">contact</router-link> </div>'
};

let ContactList = {
  // add <router-view> in order to load children route of path='/contact'
  template: '<div>this is contact list, click <router-link to="/contact/add">Add Contact In sub Router-View</router-link> here to add contact<p><router-view></router-view></p> Or Click <router-link to="/addcontact">Add Contact In Current Router-View</router-link></div>'
};

let ContactAdd = {
  template: '<div>Contact Add</div>'
}

let router = new VueRouter({
  routes: [{
    path: '/',
    redirect: 'home',
    component: Layout,
    children: [{
        path: 'home',
        component: Home
      },
      {
        path: 'contact',
        component: ContactList,
        children: [{
          path: 'add',
          component: ContactAdd
        }]
      },
      {
        path: 'addcontact', // or move ContactAdd as direct child route of path=`/`
        component: ContactAdd,
      }
    ]
  }]
});

new Vue({
  el: '#app',
  components: {
    'App': {
      template: '<div><router-view></router-view></div>'
    },
  },
  router
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vue-router@3.0.1/dist/vue-router.js"></script>
<section id="app">
  <app></app>
</section>