最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.js: Router-link with a function inside - Stack Overflow

programmeradmin2浏览0评论

I have notifications in my app which I get from API. Each notification has a parameter "notification_type". By clicking on the notification, the user can be addressed to the different pages with different content depending on the notification_type. In the ponent of the notification I have the router link which must lead to different ponents(pages for the user).

<template>
 <div>
  <router-link to="/#">
    <p>
    <span v-html="item.message"></span>
    </p>
  </router-link>
 </div>
</template>

<script>
export default {
   props: ['item']
}
</script>

I supposed that instead of '/#' I need to pass a function with conditions. For example, if notification_type is equal to "user_subscribed", then the user will be addressed to the page of the follower. If the notification_type will be equal to "ment_created", than the user will be addressed to the post page with ments. I understand the logic but I am struggling to implement this.

I have notifications in my app which I get from API. Each notification has a parameter "notification_type". By clicking on the notification, the user can be addressed to the different pages with different content depending on the notification_type. In the ponent of the notification I have the router link which must lead to different ponents(pages for the user).

<template>
 <div>
  <router-link to="/#">
    <p>
    <span v-html="item.message"></span>
    </p>
  </router-link>
 </div>
</template>

<script>
export default {
   props: ['item']
}
</script>

I supposed that instead of '/#' I need to pass a function with conditions. For example, if notification_type is equal to "user_subscribed", then the user will be addressed to the page of the follower. If the notification_type will be equal to "ment_created", than the user will be addressed to the post page with ments. I understand the logic but I am struggling to implement this.

Share Improve this question asked Jan 29, 2018 at 14:38 Olga BOlga B 5132 gold badges11 silver badges36 bronze badges 1
  • vuejs/v2/api/#v-bind – str Commented Jan 29, 2018 at 14:41
Add a ment  | 

1 Answer 1

Reset to default 10

You can implement like this:

<template>
 <div @click="redirectUser(item.notification_type)">
    <p>
    <span v-html="item.message"></span>
    </p>
 </div>
</template>

<script>
export default {
   props: ['item'], 
   methods: {
     redirectUser (notificationType) {
       if (notificationType === 'user_subscribed') {
         this.$router.push('/your-custom-route')
       } else if (notificationType === 'ment_created') {
         this.$router.push('/awesome-ments')
       }
     }
   }
}
</script>
发布评论

评论列表(0)

  1. 暂无评论