请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

老师,为什么在调用自组件的时候删除方法不能删除父组件的数据

<template>
  <div>
    <div>
      <input v-model="inputValue" />
      <button @click="handleSubmit" @delete="handleDelete">提交</button>
    </div>
    <ul>
      <todo-item
        v-for="(item, index) of list"
        :key="index"
        :content="item"
        :index="index"
        @click="handleDelete(index)"
      >
      </todo-item>
    </ul>
  </div>
</template>

<script>
import TodoItem from './components/TodoItem'
export default {
  components: {
    'todo-item': TodoItem
  },
  data () {
    return {
      inputValue: '',
      list: []
    }
  },
  methods: {
    handleSubmit () {
      this.list.push(this.inputValue)
      this.inputValue = ''
    },
    handleDelete (index) {
      this.list.splice(index, 1)
    }
  }
}
</script>

<style>
</style>

老师好,为什么handleDelete(index)删除不了父组件list里面的数据

正在回答

插入代码

1回答

Dell 2019-05-05 00:26:13

@click="handleDelete(index)" 改成@click="handleDelete",其实也不行,作用域不对。你为什么要这么做呢?我多次强调,子组件务必不要动父组件的内容

0 回复 有任何疑惑可以回复我~
  • 提问者 目訫 #1
    我就是好奇,试一试,看会发生什么,再出现这种状况,我就知道了嘛
    回复 有任何疑惑可以回复我~ 2019-05-05 00:40:24
  • 提问者 目訫 #2
    老师,这个handleDelete方法不是父组件的方法吗,我在调用子组件的时候绑定的方法啊,为什么会变成子组件更改父组件的内容呢?这个地方不是很理解,老师帮忙解答一下。
    回复 有任何疑惑可以回复我~ 2019-05-05 00:45:33
  • Dell 回复 提问者 目訫 #3
    @click="handleDelete(index)" 你这个写法不对啊,传的不是函数,是函数执行结果
    回复 有任何疑惑可以回复我~ 2019-05-05 09:52:11
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号