1.1 模仿Vue官网首页 创建首页和文档页

[toc]


创建首页和文档页

创建src/view目录以及文件Docs.vue Home.vue

开始创建官网

  • Home.vue
    • TopNav: 左边是 logo,右边是 menu
    • Banner: 文件介绍 + 开始按钮
  • Docs.vue

    • TopNav: 复用
    • Content: 左边是 aside,右边是 main

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      
      src
      ┣ assets
      ┃ ┗ logo.png
      ┣ components
      ┃ ┗ TopNav.vue
      ┣ styles
      ┃ ┗ index.scss
      ┣ views
      ┃ ┣ Docs.vue
      ┃ ┗ Home.vue
      ┣ App.vue
      ┣ env.d.ts
      ┗ main.ts

确保所有组件都有<script>标签,否则在VSCode中可能不能识别引入路径


封装 Topnav


完善样式

完善 Home.vue 样式

H1 + H2 + 两个链接

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<template>
  <div class="banner">
    <h1>Vueel 3 demo</h1>
    <h2>一个基于 Vue 3 的UI组件库</h2>
    <p class="actions">
      <a href="https://www.github.com/xmasuhai">GitHub</a> |
      <router-link to="/docs">开始</router-link>
    </p>
  </div>
</template>

<script lang="ts">
import TopNav from '@/components/TopNav.vue';

export default {
  name: 'Home',
  components: {
    TopNav
  }
};
</script>

<style lang="scss" scoped>
@use "sass:math";

$one-line-height: 28px;
.banner {
  padding: 100px 0;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  background: darkolivegreen;

  > .actions {
    padding: 8px 0;

    a {
      margin: 0 8px;
      background: #fff;
      display: inline-block;
      height: $one-line-height;
      line-height: $one-line-height;
      border-radius: math.div($one-line-height, 2);
      padding: 0 8px;
    }
  }
}
</style>

完善 Docs.vue 样式

边栏 + 主内容

主做手机,兼容PC

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<template>
  <div class="layout">
    <div class="content">
      <aside></aside>
      <main></main>
    </div>
  </div>
</template>

<script setup lang="ts">
import {inject, Ref} from 'vue';

let asideVisible = inject<Ref<boolean>>('asideVisible');
</script>

<script lang="ts">
import TopNav from '@/components/TopNav.vue';

export default {
  name: 'Docs',
  components: {TopNav}
};
</script>

<style lang="scss" scoped>
.layout {
  display: flex;
  flex-direction: column;
  height: 100vh;

  > .content {
    display: flex;
    flex-grow: 1;
    padding-top: 60px;
    padding-left: 156px;
    @media (max-width: 500px) {
      padding-left: 0;
    }

    > aside {
      flex-shrink: 0;
      position: fixed;
      top: 0;
      left: 0;
      background: cornflowerblue;
      width: 150px;
      padding: 70px 16px 16px;

    }

    > main {
      flex-grow: 1;
      padding: 16px;
      background: lightgreen;
      overflow: auto;
    }
  }

}

</style>

点击切换 aside 边栏

点击一次显示,再点击一次隐藏,使用 provide 和 inject 实现切换功能

  • menuVisible重命名为asideVisible
  • 使用一个变量asideVisible,表示边栏是否被显示
  • 变量asideVisible不可放在Topnav.vue组件中,因为Docs.vue不能访问变量
  • 变量asideVisible不可放在Docs.vue组件中,因为Docs.vue隐藏时不能访问变量
  • 变量asideVisible必须放在App.vue组件中
    • 并且通过provide/inject标记下变量asideVisible是可以被子组件访问的

点击切换 aside 边栏


App.vue中提供控制变量

使用 单文件组件<script setup>

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<script setup lang="ts">
import {provide, ref} from 'vue';

const asideVisible = ref(false);
provide('xxx', asideVisible);
</script>

<script lang="ts">
export default {
  name: 'App'
};
</script>

<template>
  <router-view></router-view>
</template>
  • <script setup>是在单文件组件 (SFC) 中使用组合式 API 的编译时语法糖
  • 里面的代码会被编译成组件 setup() 函数的内容