2

I am facing a issue in my nuxt projct.

when i route the page by using nuxt-link, it doesn't render component in my page, i guess this is not making fetch call.

but when i use normal a href link, my page is working fine. everything is in place.

here is the link in a blog listing page component

// blog listing page snippet

<template>
  <div>
    <div v-for="blog in blogs.response.posts" :key="blog.id" class="col-md-3">
      <nuxt-link :to="`/blogs/${blog.id}`" class="theme-blog-item-link"> Click to View Blog </nuxt-link>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      blogs: [],
    }
  },
  async fetch() {
    this.blogs = await fetch('https://www.happyvoyaging.com/api/blog/list?limit=4').then((res) => res.json())
  },
}
</script>

but this works fine with if i replace nuxt-link with a href tag

<a :href="`/blogs/${blog.id}`" class="theme-blog-item-link">
  Click to View Details
</a>

By click to that link, i want to view the detail of the blog by given id. that is _id.vue, code for that page is below.

//This is Specific Blog Details page code

<template>
  <div class="theme-blog-post">
    <div v-html="blogs.response.description" class="blogdesc"></div>  
  </div>
</template>

<script>
 export default {
   data(){
    return {
      blogs: []
    }
  },
  async fetch() {
    const blogid = this.$route.params.id
    this.blogs = await fetch('https://www.happyvoyaging.com/api/blog/detail?id='+blogid+'').then((res) => res.json())
  },
}
</script>

problem is on blogdetails page, where routing through nuxt-link not rendering the components but by normal a href link, it works fine

I am getting this error in console

 vue.runtime.esm.js?2b0e:619 [Vue warn]: Unknown custom element: <PageNotFound> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

found in

    ---> <Error> at layouts/error.vue
           <Nuxt>
             <Layouts/default.vue> at layouts/default.vue
               <Root>
kissu
  • 40,416
  • 14
  • 65
  • 133
  • There is no `` in given template? – T J May 11 '21 at 13:29
  • Why needed in destination template? routing to given template using doesn't work but by using tag, it works fine – vinay thakur May 11 '21 at 13:31
  • Please share the code that doesn't work. – T J May 11 '21 at 13:32
  • I have updated the question to make it clear, I have already shared the code. there are two pages, blog listing and blog details, from blog listing page, i am routing to blog details page by using . – vinay thakur May 11 '21 at 13:40
  • Please give names to your snippets of code and give a wider range, for example where is `blog.id` taken? What's the remaining logic aside this? Is the one with `fetch()` supposed to be the "details" page? You should pass it a specific blog, not the whole list of blogs. – kissu May 11 '21 at 13:49
  • @kissu - hi, i updated the question with more details, yes one with fetch() is the details page. – vinay thakur May 11 '21 at 14:02
  • Alright, there is a CORS issue during the initial call (on my side: `http://localhost/`). – kissu May 11 '21 at 15:45
  • @kissu as i am very new to NUXT or Vue. I am not able to understand how to solve this problem, can you help me here. – vinay thakur May 11 '21 at 16:08
  • CORS are totally unrelated as explained in my answer. Moreover, if you have a CORS issue on top of your question, you should create a new question about CORS only (1 question per post, especially when totally unrelated). – kissu May 11 '21 at 16:17
  • @kissu I tried your answer and its not working.. i am getting same issue. I tried to check my API with postman & also from my system, it seems its working, there is no CORS issue. but issue is something different. its like with nuxt-link, api call is not working, but if i use static data in page, it seems to work fine. – vinay thakur May 13 '21 at 09:08
  • How is my answer not working? Want me to host it to show you that it is actually working. Will maybe double check your API afterwards. – kissu May 13 '21 at 09:17
  • I checked your code and i believe that it must be working, but somehow this is not working in my project, it might be some configuration issue in my project. it simply doesn't fire fetch event whenever route using nuxt-link. as i am new to nuxtjs, i really not able to understand the issue at all. it would be great, if i can show you my entire project and then you might understand the issue. – vinay thakur May 13 '21 at 11:02
  • You can always share me a github repo yeah. – kissu May 13 '21 at 12:10
  • I will try to share it tonight. – vinay thakur May 13 '21 at 13:53
  • @kissu , I have added my learning project on github repo. find link below. as wasn't working, so i was using a href tags, but you will understand the problem i guess. i think, that issue is everywhere in my project, like i have added auth and when it redirect after successful authentication, it gives me similar issue. kinldy check that. this is the link to gitrepo https://github.com/imthegrv/hvtours – vinay thakur May 14 '21 at 12:43

1 Answers1

3

Since your API requires some CORS configuration, here is a simple solution with the JSONplaceholder API of a index + details list collection.

test.vue, pretty much the blog listing in your case

<template>
  <div>
    <div v-if="$fetchState.pending">Fetching data...</div>
    <div v-else>
      <div v-for="item in items" :key="item.id">
        <nuxt-link :to="`/details/${item.id}`"> View item #{{ item.id }}</nuxt-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [],
    }
  },
  async fetch() {
    const response = await fetch('https://jsonplaceholder.typicode.com/users')
    this.items = await response.json()
  },
}
</script>

details.vue, this one needs to be into a pages/details/_id.vue file to work

<template>
  <div>
    <button @click="$router.push('/test')">Go back to list</button>
    <br />
    <br />

    <div v-if="$fetchState.pending">Fetching details...</div>
    <div v-else>{{ details.email }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      details: {},
    }
  },
  async fetch() {
    const response = await fetch(`https://jsonplaceholder.typicode.com/users/${this.$route.params.id}`)
    this.details = await response.json()
  },
}
</script>

As you can see, I do only use async/await here and no then for consistency and clarity.

Try this example, then see for fixing the CORS issue. The latter is backend only and not anyhow related to Nuxt. A quick google search may give you plenty of results depending of your backend.

kissu
  • 40,416
  • 14
  • 65
  • 133
  • Hi Mate, I posted above git repo link, whenever you find a little from your precious time, try to check that what kind of issue and how to solve that. i tried everything found on google, nothing seems working for me. – vinay thakur May 15 '21 at 21:15
  • Alright, so I checked your repo. I submitted a pull request here: https://github.com/imthegrv/hvtours/pull/2 There was 3 000 000 useless lines that are now removed from git. Fixed some little things. There is a **lot** of stuff to fix. Which is totally out of scope of this Stackoverflow question. You will need to take a good course or some coaching to improve this app and make it production ready. Not sure on what is your profile and if it's your daily app at work or something but it needs a lot of love. The pull request fixes some basic errors and also fixes the main issue of `/en/blogs`. – kissu May 17 '21 at 01:51
  • 1
    Thanks for your suggestion kissu and thanks for the support, yes during this lockdow, am trying my best to learn this language by converting my php codeigniter website frontend on vue and nuxt, a lot to learn. took a course on Udemy, hope if it helps. if any issue arise, will post and ask for support. – vinay thakur May 17 '21 at 20:33
  • 1
    Maybe try to focus on JS basics at first, then Vue, then Nuxt. Bad idea to start from Nuxt since it requires knowledge in the other parts. – kissu May 18 '21 at 05:21