Vue,如何调用组件

时间:2017-07-21 12:49:34

标签: php vue.js components

我正在尝试从我的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;
        }
    );

}
});

1 个答案:

答案 0 :(得分: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
    })
  }
})