使用Vue.js部署博客网站

首先,需要安装 Vue CLI,然后使用它来创建一个 Vue 3 项目。在命令行中输入以下命令:

1
2
3
4
npm install -g @vue/cli
vue create my-blog
cd my-blog
vue add router

接下来,修改 src 目录下的文件以实现博客站点的功能。

  1. src 目录下创建一个名为 api 的文件夹,然后在其中创建一个名为 index.js 的文件。在这个文件中,我们将定义一个函数来获取站点信息、文章分类导航和文章列表。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// src/api/index.js
import axios from 'axios';

export async function getSiteInfo() {
const response = await axios.get('https://api.example.com/site-info');
return response.data;
}

export async function getCategories() {
const response = await axios.get('https://api.example.com/categories');
return response.data;
}

export async function getArticles(categoryId) {
const response = await axios.get(`https://api.example.com/articles?category=${categoryId}`);
return response.data;
}
  1. 修改 src/App.vue 文件,添加路由和组件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<template>
<div id="app">
<router-view></router-view>
</div>
</template>

<script>
import Home from './components/Home.vue';
import Category from './components/Category.vue';
import Article from './components/Article.vue';

export default {
name: 'App',
components: {
Home,
Category,
Article,
},
};
</script>
  1. src/router 目录下的 index.js 文件中,添加路由配置。
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
import { createRouter, createWebHistory } from 'vue-router';
import Home from '../views/Home.vue';
import Category from '../views/Category.vue';
import Article from '../views/Article.vue';

const routes = [
{
path: '/',
redirect: '/home',
},
{
path: '/home',
name: 'Home',
component: Home,
},
{
path: '/category/:id',
name: 'Category',
component: Category,
},
{
path: '/article/:id',
name: 'Article',
component: Article,
},
];

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

export default router;
  1. 创建 src/components 目录下的 Home.vueCategory.vueArticle.vue 文件,分别用于显示首页、文章分类页面和文章详情页面。在这些文件中,我们需要调用上面定义的 API 函数来获取数据,并将数据绑定到模板中。

Home.vue:

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
<template>
<div>
<h1>{{ siteInfo.title }}</h1>
<ul>
<li v-for="category in categories" :key="category.id">
<router-link :to="{ name: 'Category', params: { id: category.id } }">{{ category.name }}</router-link>
</li>
</ul>
<ul>
<li v-for="article in articles" :key="article.id">
<router-link :to="{ name: 'Article', params: { id: article.id } }">{{ article.title }}</router-link>
</li>
</ul>
</div>
</template>

<script>
import { getSiteInfo, getCategories, getArticles } from '../api';

export default {
data() {
return {
siteInfo: {},
categories: [],
articles: [],
};
},
async created() {
this.siteInfo = await getSiteInfo();
this.categories = await getCategories();
this.articles = await getArticles();
},
};
</script>

Category.vue:

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
<template>
<div>
<h1>{{ category.name }}</h1>
<ul>
<li v-for="article in articles" :key="article.id">
<router-link :to="{ name: 'Article', params: { id: article.id } }">{{ article.title }}</router-link>
</li>
</ul>
</div>
</template>

<script>
import { getArticles } from '../api';

export default {
data() {
return {
category: {},
articles: [],
};
},
async created() {
this.category = this.$route.params;
this.articles = await getArticles(this.category.id);
},
};
</script>

Article.vue:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<template>
<div>
<h1>{{ article.title }}</h1>
<p>{{ article.content }}</p>
</div>
</template>

<script>
import { getArticle } from '../api';

export default {
data() {
return {
article: {},
};
},
async created() {
this.article = await getArticle(this.$route.params.id);
},
};
</script>

现在,运行 npm run serve 来启动你的博客站点。请注意,你需要将上述代码中的 API URL 替换为实际的第三方 API URL。

🧑🏻‍💻

Mr. Du

前端工程师,热爱设计系统与响应式艺术。相信代码应该像水一样适应容器。