你可以在Vue.js中点击链接后添加URL参数如何实现?

huangapple go评论78阅读模式
英文:

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

问题

我在侧边栏上有标题链接,它们可以在主内容上进行滚动监听,当我点击它们时当然会将我带到主内容中引用的标题处。我有来自JSON文件的数据,当用户点击它们时,我想将这些标题添加到当前URL中。

假设我有以下标题(这包含在我的JSON对象中:headings.text):

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

当用户点击标题1时,我希望当前的URL www.something.com 变成 www.something.com/Heading1,其他标题也类似。

在Vue中,我该如何实现这个功能呢?

这是我目前的代码片段:

   <b-list-group-item :href="`#heading-${headingHash(headings.text)}`"> <!--标题引用部分-->
              <span>
                <b>{{ index + 1 }}.</b> {{ headings.text }} <!--标题本身-->
              </span>
   </b-list-group-item>

感谢帮助!谢谢!

英文:

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.

Let's say I have the headings(this is contained in my json object: headings.text):

  1. Heading 1
  2. Heading 2
  3. Heading 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.

How can I achieve this in Vue?

This is what I have so far.

   &lt;b-list-group-item :href=&quot;`#heading-${headingHash(headings.text)}`&quot;&gt; &lt;--Heading reference.
              &lt;span&gt;
                &lt;b&gt;{{ index + 1 }}.&lt;/b&gt; {{ headings.text }} &lt;-- Heading itself.
              &lt;/span&gt;
   &lt;/b-list-group-item&gt;

Would appreciate some help. Thanks!

答案1

得分: 1

你可以使用Vue Router。具体来说,你可以定义一个点击事件函数,并使用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>
英文:

You can use Vue Router. Namely, you can define a click event function and use Vue Router's programmatic navigation to navigate to the route you specify.

&lt;template&gt;
  &lt;b-list-group-item
    @click=&quot;handle(headings.text)&quot;
    :href=&quot;`#heading-${headingHash(headings.text)}`&quot;
  &gt;
    &lt;span&gt;
      &lt;b&gt;{{ index + 1 }}.&lt;/b&gt; {{ headings.text }}
    &lt;/span&gt;
  &lt;/b-list-group-item&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  methods: {
    handle(heading) {
      this.$router.push({
        path: `Heading${heading}`
      })
    }
  }
}
&lt;/script&gt;

答案2

得分: 0

Sure, here's the translated content:

如果你想根据href属性来改变URL,你需要使用a标签。

否则,你可以通过编程的方式来实现。所以,不是将href添加到b-list-group-item,而是可以添加一个点击事件,该事件将触发一个函数:

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

而你的方法可以是这样的:

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

If you want the url to change based on a href attribute, you need to use an a tag.

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:

&lt;b-list-group-item @click&quot;changeUrl(headings.text)&quot;&gt;

And your method could be something like:

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

huangapple
  • 本文由 发表于 2020年1月6日 16:05:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定