简体   繁体   English

VueJS:单击后渲染新组件

[英]VueJS: render new component after click

I'd like to render new component in vue.js as if it's new page.我想在 vue.js 中渲染新组件,就好像它是新页面一样。

I'm trying to do it with something called "dynamic component"我正在尝试使用称为“动态组件”的东西来做到这一点

parent : Post.vue级:Post.vue
child : Detail.vue孩子: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.问题是我必须将点击的帖子的 ID 发送到详细信息。

Here's some of my code.这是我的一些代码。

Post.vue 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细节.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 .我觉得我可以用propsv-if做到这一点。 Can someone help me?有人能帮我吗? Thanks!谢谢!

Once a post is clicked, pass post id to the click handler.单击帖子后,将帖子 ID 传递给单击处理程序。 In the click handler, route to detail.vue passing post id as route param.在点击处理程序中,路由到 detail.vue,将 post id 作为路由参数。

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.如果您在应用程序中正确设置了 vue 路由器并有一个有效的详细路线,这将起作用。

You can access the post id in details component as follows:您可以在 details 组件中访问 post id,如下所示:

created() {
    this.postId = this.$route.params.id;
}

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.我会看一下<component> ,它接受一个 prop :to并渲染一个组件,这对于标签之类的东西很有用,您可以在页面上的相同位置渲染不同的组件,而无需重新加载整个页面。 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.见这里: https ://v2.vuejs.org/v2/guide/components-dynamic-async.html 这对您来说似乎是一个非常好的用例,只需将您需要的道具传递给component即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM