Nuxt에서 API 호출 리팩토링
                                            
                                                
                                                
                                                
                                                
                                                
                                                 4024 단어  vuenuxtapijavascript
                    
API에서 가져온 데이터를 head 태그에 사용해야 하는 경우 문제가 발생하여 문서를 좀 더 살펴보았습니다.
Nuxt의 API에서 데이터를 가져오는 가장 좋은 방법은 the fetch() hook 입니다!
말이된다.
리팩토링한 후 내 스크립트 태그가 훨씬 더 깨끗해 보이고 기본 레이아웃의 구성 요소에 the keep-alive attribute이 설정되어 페이지 변경이 훨씬 빨라졌습니다.
홈 페이지 구성 요소 스크립트 태그는 현재 다음과 같습니다.
export default {
  data () {
    return{
      home: {},
      posts: [],
      projects: []
    }
  },
  async fetch() {
    this.home = await this.$axios.$get("/home-page")
    this.posts = await this.$axios.$get("/posts?_limit=4")
    this.projects = await this.$axios.$get("/projects?_limit=2")
  },
   head() {
      return {
        title: this.home.title,
        meta: [
          // hid is used as unique identifier. Do not use `vmid` for it as it will not work
          {
            hid: 'description',
            name: 'description',
            content: this.home.meta_description
          }
        ]
      }
    },
    fetchOnServer: true
}
                Reference
이 문제에 관하여(Nuxt에서 API 호출 리팩토링), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/cwraytech/part-6-refactoring-api-calls-in-nuxt-210c텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)