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
1 Answer
Reset to default 10You 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>