【问题标题】:can't get $emit to pass prop to parent in vuejs无法让 $emit 在 vuejs 中将 prop 传递给父级
【发布时间】:2019-04-19 00:19:49
【问题描述】:

我正在制作一个待办事项应用程序以更好地了解 Vue,但遇到了障碍。

我已经浏览了几个 StackOverflow 问题和 Vuejs 论坛,但我不明白我做错了什么。

问题源于to-do-item 组件模板:

  <button 
    @click="$emit('remove-item', {{item.id}} )">
    Remove
  </button>

如果我将$emit 替换为不调用$emit 的组件方法,它可以正常工作,但是当我使用$emit(即使在本地组件函数中)它拒绝呈现。

我不确定这是为什么。这是我的其余代码:

Vue.component("todo-item", {
  props:["item"],
  template: `
<li>{{ item.text }} 
  <button 
    @click="$emit('remove-item', {{item.id}} )">
    Remove
   </button>
</li>`
})

let vm = new Vue({
  el:"#app",
  data: {
    text: "",
    todos: []
  },
  methods: {
    getID: (function() {
      let id = 0;
      return function() {
        return id++;
      }
    }()),
    addTodo: function() {
      this.todos.push({id: this.getID(), text: this.text});
      this.text = "";
    },
    remove: function(remove_id) {
      this.todos = this.todos.filter(({id}) => id != remove_id);
    }
  }
})
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.min.js"></script>
<div id="app">
  <div id="container">
    <main>
      <ul>
         <todo-item
                    v-for="todo in todos"
                    :item="todo"
                    @remove-item="remove"
                   >
          </todo-item>
      </ul>
    </main>
  <div id="add-field">
  <input v-model="text" /> <button id="add" @click="addTodo">Add Todo</button>
    </div>
    </div>
</div>

【问题讨论】:

    标签: javascript vue.js vue-component


    【解决方案1】:

    问题是您试图在 javascript-executed 属性中使用模板语法:

    <button 
        @click="$emit('remove-item', {{item.id}} )">
    

    修复它,它应该可以工作:

    Vue.component("todo-item", {
      props:["item"],
      template: `
    <li>{{ item.text }} 
      <button 
        @click="$emit('remove-item', item.id )">
        Remove
       </button>
    </li>`
    })
    
    let vm = new Vue({
      el:"#app",
      data: {
        text: "",
        todos: []
      },
      methods: {
        getID: (function() {
          let id = 0;
          return function() {
            return id++;
          }
        }()),
        addTodo: function() {
          this.todos.push({id: this.getID(), text: this.text});
          this.text = "";
        },
        remove: function(remove_id) {
          this.todos = this.todos.filter(({id}) => id != remove_id);
        }
      }
    })
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.min.js"></script>
    <div id="app">
      <div id="container">
        <main>
          <ul>
             <todo-item
                 v-for="todo in todos"
                 :item="todo"
                 @remove-item="remove"
                 >
              </todo-item>
          </ul>
        </main>
      <div id="add-field">
          <input v-model="text" /> 
          <button id="add" @click="addTodo">Add Todo</button>
      </div>
    </div>

    希望这会有所帮助!

    【讨论】:

    • 我认为这很简单,但我的头撞到了墙上!非常感谢!
    • 没问题!很高兴我能帮忙
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2019-07-31
    • 2020-06-21
    • 2020-05-22
    • 2019-08-28
    相关资源
    最近更新 更多