英文:
How can I add a URL param after a link is clicked in Vue js?
问题
我在侧边栏上有标题链接,它们可以在主内容上进行滚动监听,当我点击它们时当然会将我带到主内容中引用的标题处。我有来自JSON文件的数据,当用户点击它们时,我想将这些标题添加到当前URL中。
假设我有以下标题(这包含在我的JSON对象中:headings.text
):
- 标题1
- 标题2
- 标题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
):
- Heading 1
- Heading 2
- 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.
<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!
答案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.
<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>
答案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:
<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)}`;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论