简体   繁体   中英

how to redirect from vue router-link using query?

How to add a query to the router-link so the user will be redirected to the comment under a post by clicking on the link from the notification?

API gives us an anchor of the notification and id of the comment. When the DOM renders a page, it loads a post firstly, then comments.

Here is a component of the notification:

<template>
 <div>
  <span>
   <i class="g-font-size-18 g-color-gray-light-v1"></i>
    </span>
     <router-link :to="linkFromNotification(item)
                  @click.native="linkFromNotification(item.notification_type)">
    <p>
     <span v-html="item.message"></span>
    </p>
  </router-link>
 </div>
</template>

<script>
import {mapActions, mapGetters} from 'vuex'


 export default {
 props: ['item'],
 computed: {
 ...mapGetters([
 'getNotifications'
  ])
 },
 methods: {
 ...mapActions([
 'readNotification'
 ]),
 linkFromNotification (item) {
    if (item.notification_type === 'user_subscribed') {
      return {name: 'person', params: {id: item.object_id}}
    } else if (['comment_created', 'answer_selected', 'answer_created'].includes(item.notification_type)) {
      return {name: 'show_post', params: {id: item.object_id}}
    } else if (item.notification_type === 'user_coauthored') {
      return {name: 'show_post', params: {id: item.object_id}}
     }
    }
   }
  }
 </script>

If you mean url queries you can use key query in the object you are returning, If you mean a hash "#" to be added to the link you can use the key hash. for example:

{name: 'person', params: {id: item.object_id}, query:{name:"Mohd"}, hash:"214"}

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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