vue-router 命名视图

嵌套命名视图

我们也有可能使用命名视图创建嵌套视图的复杂布局。这时你也需要命名用到的嵌套 router-view 组件。我们以一个设置面板为例:

/settings/emails                                       /settings/profile
+-----------------------------------+                  +------------------------------+
| UserSettings                      |                  | UserSettings                 |
| +-----+-------------------------+ |                  | +-----+--------------------+ |
| | Nav | UserEmailsSubscriptions | |  +------------>  | | Nav | UserProfile        | |
| |     +-------------------------+ |                  | |     +--------------------+ |
| |     |                         | |                  | |     | UserProfilePreview | |
| +-----+-------------------------+ |                  | +-----+--------------------+ |
+-----------------------------------+                  +------------------------------+
  • Nav 只是一个常规组件。
  • UserSettings 是一个视图组件。
  • UserEmailsSubscriptionsUserProfileUserProfilePreview 是嵌套的视图组件。

注意我们先忘记 HTML/CSS 具体的布局的样子,只专注在用到的组件上。

代码,由于要翻墙,保存一下:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://unpkg.com/vue/dist/vue.js"></script>
  <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>

  <style>
    .us {
      display: grid;
      grid-template-columns: auto 1fr;
      grid-template-rows: auto;
      grid-template-areas:
        "header header"
        "nav content"
        "nav helper"
      ;
    }

    h2 {
      grid-area: header;
    }

    .us__nav {
      grid-area: nav;
      border: 1px dotted;
      margin-right: .75rem;
      padding: .3rem;
    }

    .us__content {
      grid-area: content;
    }

    .us__content--helper {
      grid-area: helper;
    }
  </style>
</head>

<body>

  <div id="app">
    <h1>Nested Named Views</h1>
    <router-view></router-view>
  </div>

</body>
<script>
  const UserSettingsNav = {
    template: `
<div class="us__nav">
  <router-link to="/settings/emails">emails</router-link>
  <br>
  <router-link to="/settings/profile">profile</router-link>
</div>
`
  }
  const UserSettings = {
    template: `
<div class="us">
  <h2>User Settings</h2>
  <UserSettingsNav/>
  <router-view class ="us__content"/>
  <router-view name="helper" class="us__content us__content--helper"/>
</div>
  `,
    components: { UserSettingsNav }
  }

  const UserEmailsSubscriptions = {
    template: `
<div>
	<h3>Email Subscriptions</h3>
</div>
  `
  }

  const UserProfile = {
    template: `
<div>
	<h3>Edit your profile</h3>
</div>
  `
  }

  const UserProfilePreview = {
    template: `
<div>
	<h3>Preview of your profile</h3>
</div>
  `
  }

  const router = new VueRouter({
    mode: 'history',
    routes: [
      {
        path: '/settings',
        // You could also have named views at tho top
        component: UserSettings,
        children: [{
          path: 'emails',
          component: UserEmailsSubscriptions
        }, {
          path: 'profile',
          components: {
            default: UserProfile,
            helper: UserProfilePreview
          }
        }]
      }
    ]
  })

  router.push('/settings/emails')

  new Vue({
    router,
    el: '#app'
  })

</script>

</html>

https://jsfiddle.net/posva/22wgksa3/

命名视图 | Vue Router

猜你喜欢

转载自blog.csdn.net/qq_27009517/article/details/120489808