yejineee
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
--- tags: vue-router --- # 데이터 fetching - 데이터는 언제 fetch해야할까? vue-router를 적용하기 전에는 라이프사이클 메서드인 created 메서드 안에서 data를 fetch해왔다. 이제 라우팅을 적용하였으니, navigation이 되기 전에 data를 fetch해올 수 있을 것 같다. 관련한 글이 vue-router 공식문서에 적혀있었다. 내가 좋아하는 공식문서ㅋ-ㅋ [vue-router 공식문서의 Data Fetching](https://router.vuejs.org/guide/advanced/data-fetching.html)을 보고 언제, 어떻게 data fetching을 할 것인지 정리해보고 어떻게 할 것인지를 생각해본다. 정리를 마친 후의 내 생각은, 네비게이션 후에 data를 fetch하는 것이다. 이동 자체가 느린 것은 UX적으로 좋지 않을 것 같다. 이동해서 data를 fetch한 다음에 기본적인 컴포넌트와 skeleton component를 보여주는 것이 더 좋을 것 같다. --- 컴포넌트를 렌더링하기 전에 data를 fetch하는 시점은 두 지점이 있을 것이다. 1. **네비게이션 후에 Fetching** : 네비게이션을 한 이후에, 컴포넌트의 라이프사이클 훅에서 데이타를 fetch한다. 데이타가 fetch되기 전에는 로딩 상태를 보여준다. 2. **네비게이션 전에 Fetching** : 네비게이션 전에 data를 fetch한다. 데이타가 Fetch된 이후에 네비게이션을 한다. ## 1. 네비게이션 후 Fetching 컴포넌트로 즉시 navigate하고, 렌더링한다. 그리고 컴포넌트의 `created` hook에서 데이터를 Fetch한다. 이 방식으로 하면, 데이타가 fetch되는 동안 로딩 상태를 보여줄 수 있다. 그리고 각 상태에 따라 로딩을 다르게 처리할 수 있다. 라우트는 갖고 param이 다를 경우엔 라이프사이클 훅이 호출되지 않는다. 따라서 $route를 watch해서, params가 변경되었을 때 필요한 작업들을 해줘야 한다. ```javascript const User = { template: '...', watch: { '$route' (to, from) { // 경로 변경에 반응하여... } } } ``` ```javascript export default { data () { return { loading: false, post: null, error: null } }, created () { // fetch the data when the view is created and the data is // already being observed this.fetchData() }, watch: { // 라우트가 변경되는 것을 watch로 확인하여 데이타를 fetch해야한다. -> 라우트는 같고, Params만 변경되면 컴포넌트가 재사용되어 라이프사이클 메서드가 호출되지 않기 때문이다. '$route': 'fetchData' }, methods: { fetchData () { this.error = this.post = null this.loading = true const fetchedId = this.$route.params.id // replace `getPost` with your data fetching util / API wrapper getPost(fetchedId, (err, post) => { // make sure this request is the last one we did, discard otherwise if (this.$route.params.id !== fetchedId) return this.loading = false if (err) { this.error = err.toString() } else { this.post = post } }) } } } ``` ## 2. 네비게이션 전에 Fetching 새로운 라우트로 네비게이트되기 전에 데이터를 fetch해온다. 이 방법은 들어갈 컴포넌트의 `beforeRouteEnter` 안에서 데이터를 Fetch하는 것이다. fetch가 끝나면, `next()`를 호출한다. next()는 다음 파이프라인에 있는 훅을 호출할 것이고 호출할 훅이 없으면 navigation이 이루어진다. 만약, 문제가 생기면 next(false)로 네비게이션을 취소할 수 있다. - beforeRouteEnter - 컴포넌트가 그려지기 전에 호출된다. 컴포넌트가 아직 생성되기 전이므로, this로 컴포넌트 인스턴스에 접근할 수 없다. - 이 훅에서 첫 데이터를 fetch해온다. - beforeRouteUpdate - 이 컴포넌트가 변경되어 렌더링될 때 호출된다. - 컴포넌트는 재사용된다. 예를 들어 /foo/1에서 /foo/2로 라우트가 변경되었을 때, Foo 컴포넌트는 그대로 사용하게 된다. 그리고 이 시점에 이 훅이 호출된다. - 컴포넌트가 생성된 이후의 시점이므로 this로 컴포넌트 인스턴스에 접근할 수 있다. - 이 훅에서 동적으로 라우팅이 변경될 경우, 데이타를 Fetch한다. ```javascript export default { data () { return { post: null, error: null } }, beforeRouteEnter (to, from, next) { getPost(to.params.id, (err, post) => { next(vm => vm.setData(err, post)) }) }, // when route changes and this component is already rendered, // the logic will be slightly different. beforeRouteUpdate (to, from, next) { this.post = null getPost(to.params.id, (err, post) => { this.setData(err, post) next() }) }, methods: { setData (err, post) { if (err) { this.error = err.toString() } else { this.post = post } } } } ``` ## Stale-while-revalidate SWR이라고 캐쉬된 값 가져오고, 이후에 fetch한 data로 리프레쉬해주는 방식이 있다. 이미 가져온 데이터를 사용함으로써, 유저에게 더 빠른 응답을 줄 수 있다. vue에서 SWR 개념을 사용할 수 있게 해주는 라이브러리가 swrv이다. - SWR관련 참고하면 좋은 블로그 : https://blog.logrocket.com/advanced-data-fetching-techniques-in-vue/ - SWRV : https://markus.oberlehner.net/blog/stale-while-revalidate-data-fetching-composable-with-vue-3-composition-api/ ## 출처 - [Data Fetching - vue-router](https://router.vuejs.org/guide/advanced/data-fetching.html

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully