新手如何从零开始搭建自己的博客系统: 后台管理-新增文章列表

ppylox
·5/31/2023·0 阅读
AI 摘要模型根据正文自动生成,仅供参考

在src/views下新建postList.vue和postDetail.vue,按最小化原则实现博客文章列表与详情页。postList.vue使用Vue 3的reactive定义写死的文章列表数据,通过v-for渲染标题和内容。随后在src/router/index.ts中配置路由,将/postList路径指向postList.vue组件,采用hash模式。浏览器访问对应地址即可查看文章列表页面,为后续功能扩展奠定基础。

项目搭建完成后,是时候新建第一个页面了,为了尽可能的简单,我们按最小化原则新建对应的页面,后续的页面都可以在这个基础上完成。

博客最基础的页面就是文章列表跟文章详情页面,只要有这两个页面就可以实现一个迷你博客了。

在src/views目录下新建postList.vue文件作为文章列表页面,同目录下新建postDetail.vue作为文章详情。

src/views/postList.vue:

<template>\n  <div class="page-list">\n    <div v-for="(item, index) in state.list" :key="index">\n      <div>{{ item.title }}</div>\n      <div>{{ item.content }}</div>\n    </div>\n  </div>\n</template>\n\n<script lang="ts" setup>\nimport { reactive, onMounted, computed } from "vue";\nconst state = reactive({\n  list: [\n    {\n      title: "这是第一篇文章",\n      content: "这是www.dsiab.com网站上的第一篇文章内容"\n    },\n    {\n      title: "这是第二篇文章",\n      content: "这是www.dsiab.com网站上的第二篇文章内容"\n    }\n  ],\n});\n\n</script>\n

文章列表新建完成后,下一步需要在src/router/index.ts文件中给文件列表页面添加一个路由,这个路由也就是可以通过浏览器访问的地址:

src/router/index.ts:

import {\n  createRouter,\n  createWebHashHistory,\n} from "vue-router";\n\nconst router = createRouter({\n  history: createWebHashHistory(import.meta.env.BASE_URL),\n  routes: [\n    {\n      path: "/postList",\n      component: () => import("@/views/postList.vue"),\n    }\n  ],\n});\n\nexport default router;\n

在浏览器地址栏输入一下地址,不出意外的话应该能看到如下页面:

http://127.0.0.1:7910/#/postList

此时一个最简单的文章列表已经制作完成了,当然这个数据是写死的,但是请相信这是一个很好的开始。

相关文章

评论 (0)

暂无评论,来写第一条吧