【问题标题】:VueJs SPA execute function on click buttonVueJs SPA单击按钮执行功能
【发布时间】:2020-09-16 07:27:27
【问题描述】:

我需要从邮件列表标签运行 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);

我有这个 index.html:

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

【问题讨论】:

    标签: javascript vue.js events components emit


    【解决方案1】:

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

    试试这个 sn-p:

     Vue.component('mail-list', {
      props: ['inboxmail'],
      methods: {
        refresh: function() {
          this.$emit('refresh');
        },
      },
      template:
      `
      <div>
        <h4>{{inboxmail}}</h4>
        <button @click="refresh">Refresh</button>
      </div>
      `
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 2013-10-31
      相关资源
      最近更新 更多