簡體   English   中英

向 nuxt.config.js 添加函數

[英]Add functions to nuxt.config.js

基本上我有一個應用程序,並且在生成時,我當前生成所有頁面、站點地圖和 RSS 提要,盡管我必須進行 3 次相同的 axios 調用

像這樣

nuxt.config.js

generate: {
    routes: function() {
        return axios.get(data)
            .then(res => {
            ... 
            })
    }
},
sitemap: {
    routes: function() {
        return axios.get(data)
            .then(res => {
            ... 
            })
    }
},
feed: {
   etc...
}

現在有沒有辦法可以調用一次數據並將其提供給這些模塊方法中的每一個? 我嘗試將一些函數放在 nuxt.config.js 的頂部,但在 generate 時出現錯誤

您可以嘗試將承諾保存到變量中以供重用,例如

const routesPromise = axios.get(data).then(({ data }) => data)

// snip

generate: {
  routes () {
    return routesPromise.then(data => {
      // ...
    })
},
sitemap: {
  routes () {
    return routesPromise.then(data => {
      // ...
    })
  }
},
// etc

.then()調用添加到.then()不會修改原始解析值,因此您可以根據需要多次使用它,同時只發出一個 HTTP 請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM