如何在VUE中与输入文本连接URL

发布于 2025-02-07 17:40:48 字数 928 浏览 3 评论 0原文

我需要在Vue中加入一些帮助。我有一个输入字段,其中输入一些用户名,并且想将其与数据中的URL相连。这是我的代码

<input type="text" name="text" placeholder="Username" id='username' v-model="usersrc">
<div v-for="item in storedData" :key="item.id">
                <a :href="item.link" target="_blank">
                    <p>{{item.name}}</p>
                    <p  :id="item.id + 'availability'"></p>
                </a>
            </div>

export default {
components: {
    AppLayout,
 },  
     data() {
    return {
        usersrc: this.usersrc,
        items: [
            {id:1, name: "Facebook" ,link:"https://facebook.com/${{usersrc}}"}, and so on

,我的目的是在DIV元素中使用标签中的用户名获取整个URL。当我单击它时,例如,如果输入测试,请获取https://facebook.com/test URL。 我也尝试过这样的尝试

link:"https://facebook.com/+'usersrc'"
link:"https://facebook.com/{{usersrc}}"

,但无济于事。我是Vue的新手,所以我会感谢任何帮助!

I need a little help with concatenation in Vue. I have an input field where I enter some username and I want to concatenate it with the URL in my data. Here is my code

<input type="text" name="text" placeholder="Username" id='username' v-model="usersrc">
<div v-for="item in storedData" :key="item.id">
                <a :href="item.link" target="_blank">
                    <p>{{item.name}}</p>
                    <p  :id="item.id + 'availability'"></p>
                </a>
            </div>

export default {
components: {
    AppLayout,
 },  
     data() {
    return {
        usersrc: this.usersrc,
        items: [
            {id:1, name: "Facebook" ,link:"https://facebook.com/${{usersrc}}"}, and so on

My purpose is to get the whole URL with the username in the tag in div element. When I click on it, for example if I enter test, to get the https://facebook.com/test URL.
Also I have tried like this

link:"https://facebook.com/+'usersrc'"
link:"https://facebook.com/{{usersrc}}"

But nothing works. I am new in Vue, so I would be grateful for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花桑 2025-02-14 17:40:48

您可以使用 nofollow noreferrer“ a>。

此外,我只是添加了一张检查,如果输入框中没有值,则不应显示链接。您可以根据自己的要求删除此支票。

演示

new Vue({
  el: '#app',
  data: {
    usersrc: null,
    items: [
        { id: 1, name: 'Facebook', link: 'https://facebook.com/' },
      { id: 2, name: 'Instagram', link: 'https://instagram.com/' }
    ]
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <input type="text" name="text" placeholder="Username" id='username' v-model="usersrc">
  <div v-if="usersrc">
    <div v-for="item in items" :key="item.id">
      <a :href="`${item.link}${usersrc}`" target="_blank">
        <p>{{item.name}}</p>
        <p :id="item.id + 'availability'"></p>
      </a>
    </div>
  </div>
</div>

You can directly achieve this in the template by using Template literals.

In addition, I just added a check that if there is no value in input box then links should not be shown. You can remove this check based on your requirement.

Demo :

new Vue({
  el: '#app',
  data: {
    usersrc: null,
    items: [
        { id: 1, name: 'Facebook', link: 'https://facebook.com/' },
      { id: 2, name: 'Instagram', link: 'https://instagram.com/' }
    ]
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <input type="text" name="text" placeholder="Username" id='username' v-model="usersrc">
  <div v-if="usersrc">
    <div v-for="item in items" :key="item.id">
      <a :href="`${item.link}${usersrc}`" target="_blank">
        <p>{{item.name}}</p>
        <p :id="item.id + 'availability'"></p>
      </a>
    </div>
  </div>
</div>

猥︴琐丶欲为 2025-02-14 17:40:48

尝试

link:"'https://facebook.com/'+usersrc"

或更好地使用Alt+96

link:"`https://facebook.com/`+usersrc"

try with

link:"'https://facebook.com/'+usersrc"

or better with alt+96

link:"`https://facebook.com/`+usersrc"
甜警司 2025-02-14 17:40:48

data()函数在组件渲染时仅执行一次。因此,我们需要在发生变化时手动更新该数据。并在Backtict中使用单个卷发括号。

watch:{
 usersrc(newVal){
   this.items =  this.items.map(item=> {
        return {...item,link:`https://facebook.com/${usersrc}`})
   }
 }
}

data() function execute only once when component render. so we need to update that data manually when something is change. and also use single curly braces in backtick.

watch:{
 usersrc(newVal){
   this.items =  this.items.map(item=> {
        return {...item,link:`https://facebook.com/${usersrc}`})
   }
 }
}
遗失的美好 2025-02-14 17:40:48

这应该起作用,也许问题在其他地方?我是Juse注意到您在数据中写的:

usersrc: this.usersrc

我建议使用像空数组一样使用默认值,因为您可以通过脚本中的“此”访问数据中的内容,因此这将是多余的。

如果您用
userrc:''

This should work, maybe the problem is elsewhere ? I juste noticed that you wrote in data :

usersrc: this.usersrc

I would suggest using a default value like an empty array, because what you have in data can be accessible via 'this' in the script, so this would be redundant.

Does it work if you replace this line by
usersrc: '' ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文