如何在VUE中与输入文本连接URL
我需要在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 nofollow noreferrer“ a>。
此外,我只是添加了一张检查,如果输入框中没有值,则不应显示链接。您可以根据自己的要求删除此支票。
演示:
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 :
尝试
或更好地使用Alt+96
try with
or better with alt+96
data()函数在组件渲染时仅执行一次。因此,我们需要在发生变化时手动更新该数据。并在Backtict中使用单个卷发括号。
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.
这应该起作用,也许问题在其他地方?我是Juse注意到您在数据中写的:
我建议使用像空数组一样使用默认值,因为您可以通过脚本中的“此”访问数据中的内容,因此这将是多余的。
如果您用
userrc:''
?This should work, maybe the problem is elsewhere ? I juste noticed that you wrote in data :
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: ''
?