NuxtJS计算属性仅在导航时运行

huangapple go评论46阅读模式
英文:

NuxtJS computed property running only on navigate

问题

组件

<script>
import { mapState } from 'vuex';

export default {

layout: &#39;index&#39;,

/**
 * 运行状态分发
 */
async fetch({store}) {
    store.dispatch(&#39;api/contracts/all&#39;)
},
/**
 * 创建状态映射
 */
computed: {
    ...mapState({
        contracts: &#39;contracts&#39;
    })
}    

}
</script>


api/contracts

const axios = require("axios");

export const state = () => ({
contracts: [],
})

export const mutations = {
SET_CONTRACTS (state, contracts) {
state.contracts = contracts
}
}

export const actions = {
async all({ commit }) {
let response = await getApi(/api/products, this.$axios)
let contracts = response.data

commit(&#39;SET_CONTRACTS&#39;, contracts);

}
}

const getApi = async function (url, axios) {
let response = await axios.get(url)

return {
data: response.data
}
}


关于上述问题,如果我刷新页面,合同请求不会显示在网络选项卡中,但是,如果我在应用程序内导航到另一个页面,然后返回,合同请求就会出现在网络选项卡中。为什么会发生这种情况?合同请求应该在页面加载时加载。

明确一下,如果我通过 NuxtLink 导航到另一个页面(不刷新),然后返回,合同请求会在网络选项卡中可见,并且有效;如果我刷新页面,则合同请求不会被执行。
英文:

component

&lt;script&gt;
import { mapState } from &#39;vuex&#39;;

export default {
    
    layout: &#39;index&#39;,

    /**
     * Run State Dispatches
     */
    async fetch({store}) {
        store.dispatch(&#39;api/contracts/all&#39;)
    },
    /**
     * Create State Mapping
     */
    computed: {
        ...mapState({
            contracts: &#39;contracts&#39;
        })
    }    
}
&lt;/script&gt;

api/contracts

const axios = require(&quot;axios&quot;);

export const state = () =&gt; ({
  contracts: [],
})

export const mutations = {
  SET_CONTRACTS (state, contracts) {
    state.contracts = contracts
  }
}

export const actions = {
  async all({ commit }) {
    let response = await getApi(`/api/products`, this.$axios)
    let contracts = response.data

    commit(&#39;SET_CONTRACTS&#39;, contracts);
  }
}

const getApi = async function (url, axios) {
  let response = await axios.get(url)

  return {
    data: response.data
  }
}

I am having some issues with the above, if I refresh the page, the contract request isn't seen in network tab, however, if I navigate inside the application, to another page, and return, the contract request is there in network, why is this happening, the contract request should load when the page loads?

To be clear, if I navigate to another page via a NuxtLink(without refreshing), and return the contract request is visible in network tab, and works - if I refresh the page, the contract request isn't ran!

答案1

得分: 0

NuxtJS支持SSR(服务器端渲染),这意味着:

  • 如果您是在页面的首次加载或刷新页面,您的请求将从服务器端执行,这就是为什么您在网络标签中看不到它的原因。
  • 如果您尝试从另一个页面导航到您的页面而不刷新它,您将仅从客户端端重定向,在这种情况下,您可以在网络标签中看到您的请求。

有关更多信息,请查看:https://nuxtjs.org/docs/features/rendering-modes
您可以在组件中将'fetchOnServer'属性设置为false,以禁用从服务器端获取页面。

英文:

NuxtJS supports SSR (server side rendering) which means that :

  • If you are at the first loading of the page or you refresh the page, your request will be executed from the server side and that's why you don't see it in the network tab.
  • If you try to navigate to your page from another page without refreshing it you will be redirected only from the client side and in this case you can see your request in the Network tab.

For more info check : https://nuxtjs.org/docs/features/rendering-modes
You can set the'fetchOnServer' property to false in your component to disable fetching page from the server side.

huangapple
  • 本文由 发表于 2023年2月20日 00:32:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定