-
Notifications
You must be signed in to change notification settings - Fork 238
[Korean] head.md #48
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
Merged
[Korean] head.md #48
Changes from all commits
Commits
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 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,83 @@ | ||
# Head 태그 관리 | ||
|
||
에셋 인젝션과 마찬가지로 Head 관리도 동일한 아이디어를 사용합니다. 즉 컴포넌트의 라이프사이클에서 렌더링 `context`에 데이터를 동적으로 추가한 다음 `template`에서 해당 데이터를 보간할 수 있습니다. | ||
|
||
> 2.3.2 버전 이후에서 컴포넌트의 SSR 컨텍스트에 `this.$ssrContext`로 직접 액세스할 수 있습니다. 이전 버전에서는 `createApp()`에 SSR 컨텍스트를 전달하고 이를 루트 인스턴스의 `$options`에 노출시켜 수동으로 SSR 컨텍스트를 주입해야합니다. 그러면 자식 컴포넌트가 `this.$root.$options.ssrContext`를 통해 액세스할 수 있습니다. | ||
|
||
타이틀 관리를 위해 간단한 mixin을 작성합니다. | ||
|
||
```js | ||
// title-mixin.js | ||
function getTitle (vm) { | ||
// components can simply provide a `title` option | ||
// which can be either a string or a function | ||
const { title } = vm.$options | ||
if (title) { | ||
return typeof title === 'function' | ||
? title.call(vm) | ||
: title | ||
} | ||
} | ||
const serverTitleMixin = { | ||
created () { | ||
const title = getTitle(this) | ||
if (title) { | ||
this.$ssrContext.title = title | ||
} | ||
} | ||
} | ||
const clientTitleMixin = { | ||
mounted () { | ||
const title = getTitle(this) | ||
if (title) { | ||
document.title = title | ||
} | ||
} | ||
} | ||
// VUE_ENV can be injected with webpack.DefinePlugin | ||
export default process.env.VUE_ENV === 'server' | ||
? serverTitleMixin | ||
: clientTitleMixin | ||
``` | ||
|
||
이제 라우트 컴포넌트를 사용하여 document의 title을 제어할 수 있습니다. | ||
|
||
```js | ||
// Item.vue | ||
export default { | ||
mixins: [titleMixin], | ||
title () { | ||
return this.item.title | ||
} | ||
asyncData ({ store, route }) { | ||
return store.dispatch('fetchItem', route.params.id) | ||
}, | ||
computed: { | ||
item () { | ||
return this.$store.state.items[this.$route.params.id] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
번들 렌더러에 전달된 `template` 내부 | ||
|
||
```html | ||
|
||
|
||
<title data-segment-id="430996">{{ title }}</title> | ||
|
||
|
||
... | ||
|
||
|
||
``` | ||
|
||
**참고** | ||
|
||
- 두개의 mustache(HTML 이스케이프된 보간)를 사용해 XSS 공격을 피해야 합니다. | ||
- 렌더링하는 동안 컴포넌트에 title을 설정하지 않은 경우 `context` 개체를 만들 때 기본 title을 제공해야합니다. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
--- | ||
|
||
동일한 방법을 사용하면 이 mixin을 일반 Head 관리 유틸리티로 만들 수 있습니다. |
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.
문장끝에
입니다
를 추가하는 것이 문맥상 통일될 것 같습니다.