简体   繁体   English

在 Vue js 中单击链接后如何添加 URL 参数?

[英]How can I add a URL param after a link is clicked in Vue js?

I have heading links on my sidebar that scrollspy on my main content and of course takes me to the referenced heading in the main content when I click on them.我的侧边栏上有标题链接,可以滚动监视我的主要内容,当然,当我单击它们时,会将我带到主要内容中的引用标题。 I have data coming from a JSON file and I would like to add the headings to the current url when they're clicked.我有来自 JSON 文件的数据,我想在单击它们时将标题添加到当前 url。

Let's say I have the headings(this is contained in my json object: headings.text ):假设我有标题(这包含在我的 json 对象中: headings.text ):

  1. Heading 1标题 1
  2. Heading 2标题 2
  3. Heading 4标题 4

When the user clicks on Heading 1 , I want my current URL www.something.com to change to www.something.com/Heading1 and likewise for the other headings.当用户单击Heading 1 时,我希望我当前的 URL www.something.com更改为www.something.com/Heading1 ,其他标题也是如此。

How can I achieve this in Vue?我如何在 Vue 中实现这一点?

This is what I have so far.这是我到目前为止。

   <b-list-group-item :href="`#heading-${headingHash(headings.text)}`"> <--Heading reference.
              <span>
                <b>{{ index + 1 }}.</b> {{ headings.text }} <-- Heading itself.
              </span>
   </b-list-group-item>

Would appreciate some help.希望得到一些帮助。 Thanks!谢谢!

You can use Vue Router .您可以使用Vue Router Namely, you can define a click event function and use Vue Router's programmatic navigation to navigate to the route you specify.也就是说,您可以定义一个点击事件函数,并使用Vue Router 的编程导航来导航到您指定的路线。

<template>
  <b-list-group-item
    @click="handle(headings.text)"
    :href="`#heading-${headingHash(headings.text)}`"
  >
    <span>
      <b>{{ index + 1 }}.</b> {{ headings.text }}
    </span>
  </b-list-group-item>
</template>

<script>
export default {
  methods: {
    handle(heading) {
      this.$router.push({
        path: `Heading${heading}`
      })
    }
  }
}
</script>

If you want the url to change based on a href attribute, you need to use an a tag.如果您希望根据href属性更改 url,则需要使用a标记。

Otherwise, you can do it programmatically.否则,您可以以编程方式进行。 So, instead of adding href to b-list-group-item to could add a click event that would trigger a function:因此,与其将href添加到b-list-group-item ,还可以添加一个触发函数的点击事件:

<b-list-group-item @click"changeUrl(headings.text)">

And your method could be something like:你的方法可能是这样的:

methods: {
    changeUrl(text) {
      window.location.href = `#heading-${this.headingHash(text)}`;
    }
}

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

相关问题 如何禁用 href 链接上的“/”预设,以便在单击按钮时不会将完整的外部 URL 添加到站点链接? - How can I disable the “/” preset on a href link so that it does not add a full external URL to the site link when button is clicked? 如何向现有的 url 添加参数?…Django…Paginator - How can i add a param to the existing url?…Django…Paginator 单击链接并重定向时,如何添加活动类? - How can I add class active when a link clicked and redirect? 单击后如何隐藏我的Ajax操作链接? - How can I hide my ajax action link after it is clicked? 使用 Bootstrap Vue 在 Vue JS 上单击选项卡后如何加载组件? - How to load component after a tab is clicked on Vue JS with Bootstrap Vue? 如何在Vue.js中的4个元素之后每次添加一个元素? - How can I add a element everytime after 4 elements in Vue.js? 如何添加点击处理程序功能以在Vue中动态链接? - How i can add click handler function to dynamically link in Vue? 在 React js 中单击项目后如何显示组件? - How can I show a component after item clicked in react js? 单击按钮后如何添加成功消息? - How can I add success message after button is clicked? 基于 vue.js 中的 url 将 class 活动添加到路由器链路 - Add class active to router link based on url in vue.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM