javascript - 如何使用 VueJS 在带有按钮的图片周围显示边框?

标签 javascript html css vue.js vuejs2

我尝试使用 VueJS 在带有按钮的图片周围显示边框,此边框在我的按钮上正常显示。我明白这一点,但我不明白如何链接按钮和图像以显示图片周围的边框。

Vue.component('my-button', 'my-img' {
  template: `<button :class="classObj" @click="toggleState()">My Button</button>
  <my-img :src="image1" @click="toggleState()" :class="classObj" >`,

  props: {
    state: {
      type: String
    }
  },

  data () {
    return {
      isActive: false, 
    }
  },

  computed: {
    classObj () {
      return {
        [this.state]: this.isActive
      }
    }
  },

  methods: {
    toggleState () {
      this.isActive = this.isActive ? false : true;
    }
  }
});

const App = new Vue({
  el: '#app',
  data: {
     image1 :"http://emersontech.net/wp-content/uploads/2013/05/java.jpg",
  },

});

最佳答案

我已经更改了您的代码,通过从按钮向父组件发出事件以更改状态来实现所需的结果,因此您无需为图像创建组件,只需使用<img .../>父模板中的标记。您还必须知道父组件和子组件之间的通信是使用 props 保证的。和 this.$emit()功能。 here the working solution

关于javascript - 如何使用 VueJS 在带有按钮的图片周围显示边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52192969/

相关文章:

javascript - HTML/CSS/JS 初学者错误

html - 使用 fontawesome 图标缩进列表项

html - 焦点输入字段被虚拟键盘遮挡

html - Bootstrap 3 中的中心 <td> 和 <th> 元素

html - 具有百分比高度的 Div 结构

javascript - 使用/不使用原型(prototype)的 JS 继承

javascript - Bootstrap :Fixed nav-static-top on top but scrollable nav-collapse

javascript - 数据表未将 Json 与列绑定(bind)

javascript - 一键点击后在javascript中的两个div中设置两个不同的图像

javascript - 如何禁用chrome native "max-scroll"覆盖?