[NUXT] Navigate with Nuxt-link and Customize isclient Behavior in Nuxt and Vue.js

Source: Internet
Author: User

Because NUXT renders pages on the server, you should use the components to nuxt-link navigate between pages. Each time a page loads, can check if you ' re on the client or server and avoid doing unnecessary loading based on how T He page was rendered. This lesson walks your through using and to nuxt-link isClient navigate and avoid reloading data.

' Fetch ' can do server side rendering:

Async Fetch ({store, redirect, error}) {try  {    = await axios.get (' https:// Todos-cuvsmolowg.now.sh/todos ')    store.commit (' init ', res.data)  catch  (err {    Redirect ('/error ')  }}

Once it successfully store the data inside the store object, we don ' t need to fetch it again.

To avoid refetching the data, we can use ' isclient ' from the context.

fetch ({store, redirect, error, isclient}) {  if  (isclient) {    return   }   Try {    = await axios.get (' Https://todos-cuvsmolowg.now.sh/todos ')    store.commit (' init ') , Res.data)   Catch (err) {    Redirect ('/error ')  }}

Because This fetch method can is reused in elsewhere, so we can make it a sprated file:

Shared.js:

Import Axios from ' Axios 'function  init ({store, redirect, error, isclient}) {  if  (isclient) {    return  }  try  {    = await Axios.get (' Https://todos-cuvsmolowg.now.sh/todos ')    store.commit (' init ', Res.data)   catch  (err) {    Redirect ('/error ')  }}

Required it in side page:

<Template>  <Div>     <Nuxt-link  to= "/computed">computed</  Nuxt-link>     <articleclass= "Pa3 pa5-ns">      <ulclass= "List pl0 ml0 center mw6 ba b--light-silver br2">        <Liv-for= "Todo of Todos"class= "Ph3 pv3 bb b--light-silver">{{Todo.task}}</Li>      </ul>    </article>  </Div></Template><Script>Import {mapstate} from'Vuex'Import {init} from'./shared'Exportdefault{fetch:init, computed: {... mapstate ({todos: (state)=State.todos})}}</Script>

Here we use ' nuxt-link ' to the navigation.

Computed page should not load the Todos again, since we already has the data store in the store object.

Computed.vue:

<Template>  <Div>    <Nuxt-link to="/">Home</Nuxt-link>    <articleclass= "Pa3 pa5-ns">      <ulclass= "List pl0 ml0 center mw6 ba b--light-silver br2">        <Liv-for= "Todo of Todos"class= "Ph3 pv3 bb b--light-silver">{{Todo.task}}</Li>      </ul>    </article>  </Div></Template><Script>Import {mapstate} from'Vuex'Import {init} from'./shared'Exportdefault{fetch:init, computed: {... mapstate ({todos: (state)=State.todos})}}</Script>

[NUXT] Navigate with Nuxt-link and Customize isclient Behavior in Nuxt and Vue.js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.