javascript - VueJS 中的数据绑定(bind)以构建 URL

标签 javascript html data-binding vue.js vuejs2

我第一次使用 VueJS,非常棒。刚刚遇到了一些数据绑定(bind)问题。

  • 我需要客户输入图像,因此在输入字段中输入网址之前不应显示任何图像。目前,在等待客户提供图像时,它会显示损坏的图像。
  • 我需要使用静态 URL (amazon.com) 构建一个 URL,并将绑定(bind)图像附加到该 URL。我尝试过 v-bind:href 但没有任何喜悦

    <div class="main" id="vue-instance">
    <!-- this will be the DOM element we will mount our VueJs instance to -->
    Enter an image url:
    <input type="text" v-model="myimage">
    
    <img class="insert" style="max-height:300px" v-bind:src="myimage">
    <a href="https://amazon.com" target="_blank">Link to the image</a>
    </div>
    

这里的代码为 JSFiddle

最佳答案

您可以初始设置 myimage='' 或 null。然后,您可以使用 v-if 语句仅在 myimage 中有某些值时才显示图像和图像链接

<div v-if="myimage">
    <img class="insert" style="max-height:300px" v-bind:src="myimage">
    <a v-bind:href="'https://amazon.com/' + myimage" target="_blank">Link to the image</a>
</div>

关于javascript - VueJS 中的数据绑定(bind)以构建 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50990375/

相关文章:

javascript - 如何修复javascript数组写入问题?

Javascript 文件无法在 github 页面上运行

javascript - 在 Mobile Safari 上播放声音?

javascript - 设置重置按钮以清除文本类型的特定单个输入

wpf - 有没有像 WPF 一样支持数据绑定(bind)的跨平台 GUI 框架?

Javascript倒计时不起作用

javascript - 在 JavaScript 中对同一个变量调用多个函数

html - 对齐文本和图标 CSS

.net - 将 Grid 用作 ItemsPanelTemplate 的 ListBox 会产生奇怪的绑定(bind)错误

angular - 在 Angular 4+ 中绑定(bind)到属性/函数与变量是不是很糟糕?