Skip to content

Commit 560e087

Browse files
authored
Translated cookbook/serverless-blog.md (#690)
* translated cookbook/serverless-blog.md * removed duplicated title * Update serverless-blog.md * Update serverless-blog.md
1 parent 3889093 commit 560e087

File tree

1 file changed

+53
-56
lines changed

1 file changed

+53
-56
lines changed

src/v2/cookbook/serverless-blog.md

+53-56
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,68 @@
11
---
2-
title: Create a CMS-Powered Blog
2+
title: 创建一个基于 CMS 的博客
33
type: cookbook
44
order: 5
55
---
66

7-
# Create a CMS-Powered Blog Using Vue.js
7+
恭喜你已经发布了你的 Vue.js 网站!现在你想要在网站上快速加入一个博客,但不想新起一台服务器去部署一个 WordPress 实例 (或任何基于数据库的 CMS)。你希望只添加一些 Vue.js 的博客组件和一些路由就能搞定对吧?你想要的就是直接消费你的 Vue.js 应用中 API 来完全支撑起一个博客站。这份教程将会告诉你如何做到这一点,所以让我们来看看吧!
88

9-
So you've just launched your Vue.js website, congrats! Now you want to add a blog that quickly plugs into your website and you don't want to have to spin up a whole server just to host a Wordpress instance (or any DB-powered CMS for that matter). You want to just be able to add a few Vue.js blog components and some routes and have it all just work, right? What you're looking for a blog that's powered entirely by API's you can consume directly from your Vue.js application. This tutorial will teach you how to do just that, let's dive in!
9+
我们将会使用 Vue.js 快速构建一个基于 CMS 的博客站。它使用了 [ButterCMS](https://buttercms.com/),一个 API 优先的 CMS,让你使用 ButterCMS 仪表盘管理内容并将我们的内容 API 集成到你的 Vue.js 应用。你可以为一个新的项目或在一个已有的 Vue.js 项目使用 ButterCMS。
1010

11-
We're going to quickly build a CMS-powered blog with Vue.js. It uses [ButterCMS](https://buttercms.com/), an API-first CMS that lets you manage content using the ButterCMS dashboard and integrate our content API into your Vue.js app. You can use ButterCMS for new or existing Vue.js projects.
11+
![Butter 仪表盘](https://user-images.githubusercontent.com/160873/36677285-648798e4-1ad3-11e8-9454-d22fca8280b7.png "Butter Dashboard")
1212

13-
![Butter Dashboard](https://user-images.githubusercontent.com/160873/36677285-648798e4-1ad3-11e8-9454-d22fca8280b7.png "Butter Dashboard")
13+
## 安装
1414

15-
## Install
15+
在你的命令行中运行:
1616

17-
Run this in your commandline:
18-
19-
`npm install buttercms --save`
17+
```bash
18+
npm install buttercms --save
19+
```
2020

21-
Butter can also be loaded using a CDN:
21+
Butter 也可以通过 CDN 加载:
2222

23-
`<script src="https://cdnjs.buttercms.com/buttercms-1.1.0.min.js"></script>`
23+
```html
24+
<script src="https://cdnjs.buttercms.com/buttercms-1.1.0.min.js"></script>
25+
```
2426

25-
## Quickstart
27+
## 快速开始
2628

27-
Set your API token:
29+
设置你的 API token
2830

29-
`var butter = require('buttercms')('your_api_token');`
31+
```javascript
32+
var butter = require('buttercms')('your_api_token');
33+
```
3034

31-
Using ES6:
35+
使用 ES6
3236

3337
```javascript
3438
import Butter from 'buttercms';
3539
const butter = Butter('your_api_token');
3640
```
3741

38-
Using CDN:
42+
使用 CDN:
3943

40-
```javascript
44+
```html
4145
<script src="https://cdnjs.buttercms.com/buttercms-1.1.0.min.js"></script>
4246
<script>
4347
var butter = Butter('your_api_token');
4448
</script>
4549
```
4650

47-
Import this file into any component you want to use ButterCMS. Then from the console run:
51+
将这个文件导入到任何你想使用 ButterCMS 的组件中。然后在浏览器的命令行中运行:
4852

4953
```javascript
5054
butter.post.list({page: 1, page_size: 10}).then(function(response) {
5155
console.log(response)
5256
})
5357
```
5458

55-
This API request fetches your blog posts. Your account comes with one example post which you'll see in the response.
59+
这个 API 请求会获取你的博客文章列表。你将会在请求的响应中看到你的账户的一篇示例博文。
5660

57-
## Display posts
58-
To display posts we create a `/blog` route (using vue-router) in our app and fetch blog posts from the Butter API, as well as a `/blog/:slug` route to handle individual posts.
61+
## 展示博文
5962

60-
See the ButterCMS [API reference](https://buttercms.com/docs/api/?javascript#blog-posts) for additional options such as filtering by category or author. The response also includes some metadata we'll use for pagination.
63+
为了展示博文,我们在应用中创建了一个 `/blog` 路由 (使用 Vue Router) 并从 Butter API 获取博文列表,同样的还创建了一个 `/blog/:slug` 路由来处理单篇博文。
64+
65+
你可以翻阅 ButterCMS [API 参考文档](https://buttercms.com/docs/api/?javascript#blog-posts) 来了解更多选项,比如按分类或作者过滤。请求的响应也会包含一些用在翻页导航上的元数据。
6166

6267
`router/index.js:`
6368

@@ -86,9 +91,9 @@ export default new Router({
8691
})
8792
```
8893

89-
Then create `components/BlogHome.vue` which will be your blog homepage that lists your most recent posts.
94+
然后创建 `components/BlogHome.vue` 作为你的博客首页,列出你最近的博文。
9095

91-
```javascript
96+
```html
9297
<script>
9398
import { butter } from '@/buttercms'
9499
export default {
@@ -105,7 +110,6 @@ Then create `components/BlogHome.vue` which will be your blog homepage that list
105110
page: 1,
106111
page_size: 10
107112
}).then((res) => {
108-
// console.log(res.data)
109113
this.posts = res.data.data
110114
})
111115
}
@@ -115,18 +119,17 @@ Then create `components/BlogHome.vue` which will be your blog homepage that list
115119
}
116120
}
117121
</script>
118-
Display the result
119122

120123
<template>
121124
<div id="blog-home">
122125
<h1>{{ page_title }}</h1>
123-
<!-- Create v-for and apply a key for Vue. Example is using a combination of the slug and index -->
126+
<!-- 创建 `v-for` 并为 Vue 应用一个 `key`,该示例使用了 `slug` 和 `index` 的组合。 -->
124127
<div v-for="(post,index) in posts" :key="post.slug + '_' + index">
125128
<router-link :to="'/blog/' + post.slug">
126129
<article class="media">
127130
<figure>
128-
<!-- Bind results using a ':' -->
129-
<!-- Use a v-if/else if their is a featured_image -->
131+
<!-- 使用 `:` 绑定结果 -->
132+
<!-- 使用一组 `v-if`/`else` 判断它们是否是 `featured_image` -->
130133
<img v-if="post.featured_image" :src="post.featured_image" alt="">
131134
<img v-else src="http://via.placeholder.com/250x250" alt="">
132135
</figure>
@@ -139,14 +142,13 @@ Display the result
139142
</template>
140143
```
141144

142-
Here's what it looks like (note we added CSS from https://bulma.io/ for quick styling):
145+
这是它的样子 (注意为了快速设置样式,我们从 https://bulma.io/ 添加了 CSS):
143146

144147
![buttercms-bloglist](https://user-images.githubusercontent.com/160873/36868500-1b22e374-1d5e-11e8-82a0-20c8dc312716.png)
145148

149+
现在创建 `components/BlogPost.vue` 用来展示你的单篇博文页面。
146150

147-
Now create `components/BlogPost.vue` which will be your Blog Post page to list a single post.
148-
149-
```javascript
151+
```html
150152
<script>
151153
import { butter } from '@/buttercms'
152154
export default {
@@ -160,7 +162,6 @@ Now create `components/BlogPost.vue` which will be your Blog Post page to list a
160162
getPost() {
161163
butter.post.retrieve(this.$route.params.slug)
162164
.then((res) => {
163-
// console.log(res.data)
164165
this.post = res.data
165166
}).catch((res) => {
166167
console.log(res)
@@ -172,7 +173,7 @@ Now create `components/BlogPost.vue` which will be your Blog Post page to list a
172173
}
173174
}
174175
</script>
175-
Display the results
176+
176177
<template>
177178
<div id="blog-post">
178179
<h1>{{ post.data.title }}</h1>
@@ -189,22 +190,21 @@ Display the results
189190
</template>
190191
```
191192

192-
Here's a preview:
193+
预览效果如下:
193194

194195
![buttercms-blogdetail](https://user-images.githubusercontent.com/160873/36868506-218c86b6-1d5e-11e8-8691-0409d91366d6.png)
195196

197+
现在我们的应用已经拉取了所有博文并且可以导航到每个独立的博文。但上一篇博文/下一篇博文的链接还不工作。
196198

197-
Now our app is pulling all blog posts and we can navigate to individual posts. However, our next/previous post buttons are not working.
199+
需要注意的一点是在通过参数控制路由时,比如当用户从 `/blog/foo` 导航至 `/blog/bar` 时,我们复用了相同的组件实例。因为这两个路由渲染了相同的组件,所以比销毁老实例再创建新实例的效率更高。
198200

199-
One thing to note when using routes with params is that when the user navigates from /blog/foo to /blog/bar, the same component instance will be reused. Since both routes render the same component, this is more efficient than destroying the old instance and then creating a new one.
201+
<p class="tip">注意,用这种方式使用组件意味着这个组件的生命周期钩子将不会被调用。请移步 Vue Router 的文档了解[动态路由匹配](https://router.vuejs.org/en/essentials/dynamic-matching.html)。</p>
200202

201-
<p class="tip">Be aware, that using the component this way will mean that the lifecycle hooks of the component will not be called. Visit the Vue.js docs to learn more about [Dynamic Route Matching](https://router.vuejs.org/en/essentials/dynamic-matching.html)</p>
203+
为了解决这个问题,我们需要匹配 `$route` 对象并在路由发生变化的时候调用 `getPost()`
202204

203-
To fix this we need to watch the `$route` object and call `getPost()` when the route changes.
205+
更新 `components/BlogPost.vue` 中的 `<script>` 部分:
204206

205-
Updated `script` section in `components/BlogPost.vue`:
206-
207-
```javascript
207+
```html
208208
<script>
209209
import { butter } from '@/buttercms'
210210
export default {
@@ -218,7 +218,6 @@ Updated `script` section in `components/BlogPost.vue`:
218218
getPost() {
219219
butter.post.retrieve(this.$route.params.slug)
220220
.then((res) => {
221-
// console.log(res.data)
222221
this.post = res.data
223222
}).catch((res) => {
224223
console.log(res)
@@ -237,21 +236,21 @@ Updated `script` section in `components/BlogPost.vue`:
237236
</script>
238237
```
239238

240-
Now your app has a working blog that can be updated easily in the ButterCMS dashboard.
239+
现在你的应用就有了可工作的博客,你可以在 ButterCMS 仪表盘便捷地更新它。
241240

242-
## Categories, Tags, and Authors
241+
## 分类、标签和作者
243242

244-
Use Butter's APIs for categories, tags, and authors to feature and filter content on your blog.
243+
使用 Butter 关于分类、标签和作者的 API 来设置和过滤你的博客。
245244

246-
See the ButterCMS API reference for more information about these objects:
245+
你可以移步到 ButterCMS API 参考文档来进一步了解这些对象:
247246

248247
* [Categories](https://buttercms.com/docs/api/?ruby#categories)
249248
* [Tags](https://buttercms.com/docs/api/?ruby#tags)
250249
* [Authors](https://buttercms.com/docs/api/?ruby#authors)
251250

252-
Here's an example of listing all categories and getting posts by category. Call these methods on the `created()` lifecycle hook:
251+
这里有一个示例,列出所有分类并根据分类获取博文列表。在生命周期钩子 `created()` 中调用这些方法:
253252

254-
```
253+
```javascript
255254
methods: {
256255
...
257256
getCategories() {
@@ -278,12 +277,10 @@ created() {
278277
}
279278
```
280279

281-
## Alternative Patterns
282-
283-
An alternative pattern to consider, especially if you prefer writing only in Markdown, is using something like [Nuxtent](https://nuxtent.now.sh/guide/writing#async-components). Nuxtent allows you to use `Vue Component` inside of Markdown files. This approach would be akin to a static site approach (i.e. Jekyll) where you compose your blog posts in Markdown files. Nuxtent adds a nice integration between Vue.js and Markdown allowing you to live in a 100% Vue.js world.
284-
280+
## 替代方案
285281

286-
## Wrap up
282+
有一个替代方案,尤其在你只喜欢写 Markdown 时适用,就是使用诸如 [Nuxtent](https://nuxtent.now.sh/guide/writing#async-components) 的工具。Nuxtent 允许你在 Markdown 文件内部使用 `Vue Component`。它类似一个静态站点工具 (例如 Jekyll),让你在 Markdown 文件中撰写你的博文。Nuxtent 将 Vue.js 和 Markdown 很好地集成了起来,让你完全生活在 Vue.js 的世界里。
287283

288-
That's it! You now have a fully functional CMS-powered blog running in your app. We hope this tutorial was helpful and made your development experience with Vue.js even more enjoyable :)
284+
## 总结
289285

286+
差不多就是这些了!现在你已经在自己的应用中拥有了一个可以正常工作的 CMS 博客。我们希望这份教程可以帮助你,使你的 Vue.js 开发体验更有乐趣 :)

0 commit comments

Comments
 (0)