代码之家  ›  专栏  ›  技术社区  ›  zfrisch

无法在Vuejs中获取$emit以将属性传递给父级

  •  0
  • zfrisch  · 技术社区  · 7 年前

    我正在制作一个todo应用程序来更好地理解vue,并遇到了障碍。

    我已经讨论了几个StackOverflow问题和Vuejs论坛,但我不理解我做的错误。

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

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

    如果我替换 $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>
    1 回复  |  直到 7 年前
        1
  •  3
  •   Derek Pollard    7 年前

    问题是您试图在javascript执行属性内使用模板语法:

    <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>

    希望这有帮助!