【问题标题】:Vue, how to invoke to data from componentVue,如何从组件调用数据
【发布时间】:2017-12-27 12:49:28
【问题描述】:

我正在尝试从我的 Vue 数据中删除一行:todos,但我的方法在组件中。第二个问题是我无法修复,如果我的输入被检查了怎么办?我的方法切换返回或是否被选中,但我无法在我的模板上设置它。下面是代码:

Vue.component('todoList', {
  props: ['todoObj'],
  template: '<tr>' +
  '<td>{{todoObj.description}}</td>' +
  '<input type="checkbox" v-on:click="toggle"/>' +
  '<button v-on:click="deleteTodo">delete</button>' +
  '</tr>',
  methods: {
    toggle: function () {
        axios.post('/todo/toggleTodo', {
            'todoId': this.todoObj.id
        }).then(function (response) {
            Here will be code... I have to set to my checbox or is checked or not. This response return "yes" or "no"
        });
    },
    deleteTodo: function () {
        axios.post('/todo/deleteTodo', {
            'todoId': this.todoObj.id
        }).then(function (response) {
            console.log(response); <- here i don't know how to delete my row from table from Vue data: todos
        });

    }
}
});

这是我的休息代码:

var app = new Vue({
el: '#appTest',
data: {
    todos: [],
    todoText: ''

},
methods: {
    addTodo: function () {
        var self = this;

        axios.post('/todo/addTodo', {
            'newTodo': this.todoText
        }).then(function (response) {
            console.log(response);
            self.todos.unshift({
                    'id': response.data.id,
                    'description': response.data.description,
                    'done': response.data.done
                }
            );
            self.todoText = '';

        }).catch(function (error) {
            var errors = error.response.data.description[0];
            console.log(errors);
            self.error = errors;

        });


    },
    toggle: function () {
        console.log('toggle?');
    }
},
created: function () {
    var self = this;
    axios.get('/todo').then(function (response) {
            console.log(response.data);
            self.todos = response.data;
        }
    );

}
});

【问题讨论】:

    标签: php vue.js components


    【解决方案1】:

    这是一个示例,展示了您的 todo-list 组件的外观。

    Vue.component('todoList', {
      props: ['todoObj'],
      template: `<tr>
                  <td>{{todoObj.description}}</td>
                  <input type="checkbox" v-model="todoObj.done" @click="toggle"/>
                  <button @click="deleteTodo">delete</button>
                </tr>`,
    
      methods: {
        toggle() {
          axios.post('/todo/toggleTodo', {
            todoId: this.todoObj.id
          })
          .then(response => {
            let isChecked = response.data == 'yes'
            this.$emit('update:todoObj', Object.assign(this.todoObj, {done: isChecked}))
          })
        },
    
        deleteTodo() {
          axios.post('/todo/deleteTodo', {
            todoId: this.todoObj.id
          })
          .then(response => {
            this.$emit('delete', this.todoObj)
          })
        }
      }
    })
    

    你的主 Vue 模板应该有这个:

    <todo-list v-for="todo in todos" :todoObj.sync="todo" @delete="deleteTodo"></todo-list>
    

    vue 实例应该是这样的:

    var app = new Vue({
      el: '#appTest',
    
      data: {
        todos: [],
        todoText: '',
      },
    
      methods: {
        addTodo() {
          axios.post('/todo/addTodo', {
            'newTodo': this.todoText
          })
          .then(response => {
            let todo = response.data
            this.todos.push(todo)
            this.todoText = ''
          })
          .catch(error => {
            let errors = error.response.data.description[0]
            this.error = errors
          })
        },
        deleteTodo(todo) {
          console.log('Should delete this todo: ', todo)
        },
      },
    
      created() {
        axios.get('/todo')
        .then(response => {
          this.todos = response.data
        })
      }
    })
    

    【讨论】:

    • 感谢您的帮助。它可以工作,但是当我试图在我的视图中添加这个时:@delete="deleteTodo" 我遇到了问题,因为我使用 Laravel 作为我的框架,他认为我的 @ 意味着来自他,而不是来自 Vue。我必须以某种方式逃离@,但我不知道如何
    • 没关系,你可以用v-on:delete代替它。
    猜你喜欢
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 2022-01-22
    • 2019-05-15
    相关资源
    最近更新 更多