1

I'd like to render new component in vue.js as if it's new page.

I'm trying to do it with something called "dynamic component"

parent: Post.vue
child: Detail.vue

so, if one of the posts is clicked, Post is off and Detail is on.

The thing is I have to send clicked post's id to the Detail.

Here's some of my code.

Post.vue


<template>
  <div>
    <div v-if="loading">
      loading...
    </div>
    <div v-else class="container">
      <ul>
        <li v-for="(post, index) in filteredPosts" v-bind:key="post.no">
            <section class="post__main">
              <div @click..?? class="main__title">{{post.title}}</div>
            </section>
        </li>
      </ul>

    </div>
  </div>
</template>

<script>
    created() {
      axios.get(this.url, {
        params: {
          page: this.page,
          ord: this.ord,
          category: []
        }
      }).then(res => {
        this.posts = res.data.list;
        this.loading = false;
        this.page++;
      });

Detail.vue

<template>
    <div>
        {{contents}}
    </div>
</template>

<script>
import axios from 'axios';
export default {
    name: 'Datail',
    data() {
        return {
            req_no: null,
            contents: {}
        }
    },
    created() {
      axios.get(url, {
        params: {
          req_no: this.req_no
        }
      }).then(res => {
          this.contents = this.res.data
      });
    }
}
</script>

I feel like I can do it with props and v-if. Can someone help me? Thanks!

3
  • 'as if it's new page.' You might want to look into vue-router Commented May 16, 2019 at 9:29
  • @Frank I've looked through it, but as I have to get the data with Ajax, it seems not applicable. Commented May 16, 2019 at 9:33
  • That's not an issue Commented May 16, 2019 at 10:57

2 Answers 2

3

Once a post is clicked, pass post id to the click handler. In the click handler, route to detail.vue passing post id as route param.

like below:

  <li v-for="(post, index) in filteredPosts" v-bind:key="post.no">
      <section class="post__main">
          <div @click="onPostClick(post.id)" class="main__title">{{post.title}}</div>
      </section>
  </li>

And in your click handler:

 onPostClick(id: number) {
        this.$router.push({
            name: 'details',
            params: {
                id
            }
        });
    }

This will work provided you set up vue router correctly in your app and have a valid route for details.

You can access the post id in details component as follows:

created() {
    this.postId = this.$route.params.id;
}
Sign up to request clarification or add additional context in comments.

Comments

2

I would take a look at the <component> which takes a prop :to and renders a component, this is good for something like tabs where you are rendering different component from a the same general location on the page without reloading the whole page. See here: https://v2.vuejs.org/v2/guide/components-dynamic-async.html This seems to be a very good use case for you, just pass into the component the props you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.