Javascript VueJs SPA单击按钮执行功能

Javascript VueJs SPA单击按钮执行功能,javascript,vue.js,events,components,emit,Javascript,Vue.js,Events,Components,Emit,我需要从邮件列表标记运行refreshMailList函数,从邮件列表组件捕获单击事件 我有一个带有此组件的vue实例: Vue.component('mail-list', { props: ['inboxmail'], template: ` <div> <h4>{{inboxmail}}</h4> <button>Refresh</button> </div> ` });

我需要从邮件列表标记运行refreshMailList函数,从邮件列表组件捕获单击事件

我有一个带有此组件的vue实例:

Vue.component('mail-list', {
  props: ['inboxmail'],
  template:
  `
  <div>
    <h4>{{inboxmail}}</h4>
    <button>Refresh</button>
  </div>
  `
  });


//Creating the Vue object.
let options = {
  el: "#app",
  data: {
    pollingId: null,
    inbox: ''
  },

  created: function() {
    this.refreshMailList()
  },
methods:{
    refreshMailList: function(){
      fetch('/inbox')
      .then(response => response.json())
      .then(aJson => {
        this.inbox = aJson;
      })
    },

  } //end methods
} //end options

//ViewModel (vm)
let vm = new Vue(options);
Vue.component('mail-list'{
道具:['inboxmail'],
模板:
`
{{inboxmail}}
刷新
`
});
//创建Vue对象。
让选项={
el:“应用程序”,
数据:{
pollingId:null,
收件箱:“”
},
已创建:函数(){
这个文件名为refreshMailList()
},
方法:{
refreshMailList:函数(){
获取(“/inbox”)
.then(response=>response.json())
.然后(aJson=>{
this.inbox=aJson;
})
},
}//结束方法
}//结束选项
//视图模型(vm)
让vm=新的Vue(选项);
我有一个index.html:

      <div id="app">
        <mail-list v-bind:inboxmail="inbox" @refresh='refreshMailList'></mail-list>
      </div>

您需要从邮件列表组件内部发出事件

请尝试以下代码段:

 Vue.component('mail-list', {
  props: ['inboxmail'],
  methods: {
    refresh: function() {
      this.$emit('refresh');
    },
  },
  template:
  `
  <div>
    <h4>{{inboxmail}}</h4>
    <button @click="refresh">Refresh</button>
  </div>
  `
  });
Vue.component('mail-list'{
道具:['inboxmail'],
方法:{
刷新:函数(){
这是。$emit('refresh');
},
},
模板:
`
{{inboxmail}}
刷新
`
});

您需要从邮件列表组件内部发出事件

请尝试以下代码段:

 Vue.component('mail-list', {
  props: ['inboxmail'],
  methods: {
    refresh: function() {
      this.$emit('refresh');
    },
  },
  template:
  `
  <div>
    <h4>{{inboxmail}}</h4>
    <button @click="refresh">Refresh</button>
  </div>
  `
  });
Vue.component('mail-list'{
道具:['inboxmail'],
方法:{
刷新:函数(){
这是。$emit('refresh');
},
},
模板:
`
{{inboxmail}}
刷新
`
});