简体   繁体   English

如何在 vue.js 组件中设置方法?

[英]How to set up methods in vue.js components?

On this page在本页面

https://v2.vuejs.org/v2/guide/events.html https://v2.vuejs.org/v2/guide/events.html

I can set up methods like so我可以设置这样的方法

var example2 = new Vue({
  el: '#example-2',
  data: {
    name: 'Vue.js'
  },
  // define methods under the `methods` object
  methods: {
    greet: function (event) {
      // `this` inside methods points to the Vue instance
      alert('Hello ' + this.name + '!')
      // `event` is the native DOM event
      if (event) {
        alert(event.target.tagName)
      }
    }
  }
})

// you can invoke methods in JavaScript too
example2.greet() // => 'Hello Vue.js!'

but if I make a component, the methods don't work但是如果我制作一个组件,这些方法就不起作用

    Vue.component('ti-user-card', {
        data: function () {
            return {
                pEmail: "test@domain.com"
            };
        },
        template: '#vUserCard',
        methods : {
            mEmail : function(event) {
                window.location.href = "mailto:" + this.pEmail;
            }
        }
    });

html html

<template id="vUserCard">
     <button v-bind:click="mEmail"> 
          Email 
     </button>
</template>

<div id="app">
    <ti-user-card></ti-user-card>
</div>

how can I fix this?我怎样才能解决这个问题?

Thanks谢谢

Use <button v-on:click="mEmail">A</button> instead of <button v-bind:click="mEmail">A</button> because here you're handling an event, so you have to put v-on:click not v-bind:click or :click使用<button v-on:click="mEmail">A</button>而不是<button v-bind:click="mEmail">A</button>因为这里你正在处理一个事件,所以你必须放v-on:click不是v-bind:click:click

v-bind:someAtt="property" : property could be a props value, data object or computed property v-bind:someAtt="property" : 属性可以是props值、数据对象或计算属性

v-on:event="eventHandler" : eventHandler is a method v-on:event="eventHandler" : eventHandler 是一个方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM