forked from vuejs/vue2-ssr-docs
-
Notifications
You must be signed in to change notification settings - Fork 9
Translate basic.md #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kazupon
merged 4 commits into
open-source-translators:master
from
satotakumi:gitlocalize-85
Jun 15, 2017
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
# 基本的な使い方 | ||
|
||
## インストール | ||
|
||
```bash | ||
npm install vue vue-server-renderer --save | ||
``` | ||
|
||
このガイドでは NPM を使って説明していきますが [Yarn](https://yarnpkg.com/en/) でも大丈夫です。 | ||
|
||
#### 注意 | ||
|
||
- Node.jsのバージョンは6以上を使用することを推奨します | ||
- `vue-server-renderer` と `vue` のバージョンは一致している必要があります | ||
- `vue-server-renderer` はNode.jsのネイティブモジュールに依存しているため、Node.jsでのみ使用できます。 私たちは、将来的に他のJavaScriptランタイムで実行できるよりシンプルなビルドを提供するかもしれません。 | ||
|
||
## Vue インスタンスをレンダリング | ||
|
||
```js | ||
// ステップ 1: Vue インスタンスを作成 | ||
const Vue = require('vue') | ||
const app = new Vue({ | ||
template: `<div>Hello World</div>` | ||
}) | ||
// ステップ 2: レンダラを作成 | ||
const renderer = require('vue-server-renderer').createRenderer() | ||
// ステップ 3: Vue インスタンスをHTMLに描画 | ||
renderer.renderToString(app, (err, html) => { | ||
if (err) throw err | ||
console.log(html) | ||
// => <div data-server-rendered="true">hello world</div> | ||
}) | ||
``` | ||
|
||
## サーバと連携する | ||
|
||
Node.js で作られたサーバで使う場合はとても簡単です。例えば [Express](https://expressjs.com/): | ||
|
||
```bash | ||
npm install express --save | ||
``` | ||
|
||
--- | ||
|
||
```js | ||
const Vue = require('vue') | ||
const server = require('express')() | ||
const renderer = require('vue-server-renderer').createRenderer() | ||
server.get('*', (req, res) => { | ||
const app = new Vue({ | ||
data: { | ||
url: req.url | ||
}, | ||
template: `<div>The visited URL is: {{ url }}</div>` | ||
}) | ||
renderer.renderToString(app, (err, html) => { | ||
if (err) { | ||
res.status(500).end('Internal Server Error') | ||
return | ||
} | ||
res.end(` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head><title>Hello</title></head> | ||
<body>${html}</body> | ||
</html> | ||
`) | ||
}) | ||
}) | ||
server.listen(8080) | ||
``` | ||
|
||
## ページテンプレートを使用する | ||
|
||
Vueアプリをレンダーする際、レンダラはアプリのマークアップのみを生成します。この例では、出力を余計なHTMLページシェルでラップする必要がありました。 | ||
|
||
これをシンプル化するために、レンダラの作成時にページテンプレートを直接提供することができます。ほとんどの場合、ページテンプレートを単独のファイルに記述します。 例 `index.template.html`: | ||
|
||
```html | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head><title>Hello</title></head> | ||
<body> | ||
<!--vue-ssr-outlet--> | ||
</body> | ||
</html> | ||
``` | ||
|
||
`<!--vue-ssr-outlet-->` コメントに注目してみてください。 -- これはあなたのアプリケーションのマークアップが注入される場所です。 | ||
|
||
ファイルを読み込みVueレンダラに渡すことができます。 | ||
|
||
```js | ||
const renderer = createRenderer({ | ||
template: require('fs').readFileSync('./index.template.html', 'utf-8') | ||
}) | ||
renderer.renderToString(app, (err, html) => { | ||
console.log(html) // will be the full page with app content injected. | ||
}) | ||
``` | ||
|
||
### テンプレート展開 | ||
|
||
テンプレートはシンプルな展開にも対応しています。 次のようなテンプレートであれば: | ||
|
||
```html | ||
<html> | ||
<head> | ||
<title>{{ title }}</title> | ||
{{{ meta }}} | ||
</head> | ||
<body> | ||
<!--vue-ssr-outlet--> | ||
</body> | ||
</html> | ||
``` | ||
|
||
`renderToString` の第2引数として "描画コンテキストオブジェクト"(render context object) を渡すことで展開データを提供することができます | ||
|
||
```js | ||
const context = { | ||
title: 'hello', | ||
meta: ` | ||
<meta ...> | ||
<meta ...> | ||
` | ||
} | ||
renderer.renderToString(app, context, (err, html) => { | ||
// ページタイトルは "hello" になり、 | ||
// メタタグが注入されます。 | ||
}) | ||
``` | ||
|
||
`context` オブジェクトもVueアプリインスタンスと共有することができ、コンポーネントがテンプレート展開のためにデータを動的に追加することができます。 | ||
|
||
さらに、テンプレートは次のような高度な機能をサポートしています: | ||
|
||
- `*.vue` コンポーネントを使用する際の、重要なCSSの自動注入 | ||
- `clientManifest` を使用する際の、アセットリンクとリソースヒントの自動注入 | ||
- クライアントサイドハイドレーションのためにVuexの状態を埋め込む際にXSS防止の自動注入 | ||
|
||
関連する概念については、後でこのガイドで紹介します。 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
render
は描画するに統一しましょう!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kazupon ありがとうございます!修正しました!