Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript vuejs数据属性未定义_Javascript_Vue.js - Fatal编程技术网

Javascript vuejs数据属性未定义

Javascript vuejs数据属性未定义,javascript,vue.js,Javascript,Vue.js,我在vuejs实例中返回未定义属性,但在HTML中返回正确值时遇到问题 data: { ... userFilter: 'all', ... }, 警报将此.userFilter返回为未定义 filters: { all: function(tasks) { alert(this.userFilter); // This is undefined if(this.userFilter == 'all'){

我在vuejs实例中返回未定义属性,但在HTML中返回正确值时遇到问题

data: {
    ...
    userFilter: 'all',
    ...
},    
警报将此.userFilter返回为未定义

filters: {
    all: function(tasks) {
        alert(this.userFilter); // This is undefined
        if(this.userFilter == 'all'){
            return tasks;
        }else{
            return tasks.filter(function(task){
                return task.user_id == this.userFilter;
            });
        }
    },
}
下拉选择要筛选的用户

<select class="form-control" v-if="visibility == 'all'" v-model="userFilter">
    <option selected value="all">Showing all users tasks</option>
    <option v-for="user in users"
        value="@{{user.id}}">
        @{{user.first_name}} @{{user.last_name}}
    </option>
</select>
完整代码:

var tasks = new Vue({
    el: '#tasks',

    data: {
        tasks: [],
        users: [],
        newTask: { description: '', due_at: '', user_id: '', completed: false },
        editingTask: false,
        showAlert: false,
        loading: true,
        visibility: 'active',
        validation: [],
        showUser: null,
        authUser: {}
    },

    ready: function() {
        this.getAuthUser();
        this.getTasks();
        this.getUsers();
    },

    computed: {
        filteredTasks: function () {
            return this.$options.filters[this.visibility](this.tasks);
        },
        remaining: function() {
            return this.tasks.filter(function(task){
                return !task.completed && task.user_id == this.authUser.id;
            }).length;
        }
    },

    filters: {
        all: function(tasks) {
            return tasks;
        },
        active: function(tasks) {
            return tasks.filter(function(task){
                return !task.completed;
            });
        },
        completed: function(tasks) {
            return tasks.filter(function(task){
                return task.completed;
            });
        },
        groupByDate: function(tasks) {
            var result = {};
            for (var i = 0; i < tasks.length; i++) {
                var task = tasks[i];

                // Convert due_at date to be used as array key
                var due_at = moment(task.due_at,'YYYY-MM-DD').format('DD-MM-YYYY');

                if (result[due_at]) {
                    result[due_at].push(task);
                } else {
                    result[due_at] = [task];
                }
            }
            return result;
        },
        newDate: function(date) {
            return moment(date, 'DD-MM-YYYY').format('LL');
        },
        usersFilter: function(tasks, user_id) {
            if(this.visibility == 'all' && user_id){
                return tasks.filter(function(task){
                    return task.user_id == user_id;
                });
            }else{
                return tasks;
            }
        }
    },

    methods: {
        getTasks: function () {
            this.loading = true;
            this.$http.get('api/tasks/'+ this.visibility).success(function(tasks) {
                this.$set('tasks', tasks);
                this.loading = false;
            }).error(function(error) {
                console.log(error);
            });
        },
        getUsers: function() {
            this.$http.get('api/users/all',function(users){
                this.$set('users',users);
            });
        },
        getAuthUser: function() {
            this.$http.get('api/users/current-user').success(function(authUser) {
                this.$set('authUser',authUser);
            });
        },
        toggleVisibility: function(filter) {  
            this.visibility = filter;
            this.getTasks();
        },
        open: function(e) {  
            e.preventDefault();
            $('#add-task-modal').slideDown();
        },
        close: function(e) {  
            e.preventDefault();
            $('#add-task-modal').slideUp();
        },
        toggleAlert: function(message) {  
            this.showAlert = true;  
            $('.alert').text(message);
            $('.alert').fadeIn().delay(1000).fadeOut();
            this.showAlert = false; 
        },
        addTask: function(e) {
            e.preventDefault();

            if ( ! this.newTask) return;

            var task = this.newTask;

            this.$http.post('api/tasks', task)
                .success(function(data){
                    task.id = data.task_id;
                    task.due_at = moment(task.due_at,'DD-MM-YYYY').format('YYYY-MM-DD');

                    if(this.visibility == 'all'){
                        this.tasks.push(task);
                    }else if(this.authUser.id == task.user_id){
                        this.tasks.push(task);
                    }

                    $('.datepicker').datepicker('clearDates');

                    this.validation = [];
                    this.newTask = { description: '', due_at: '', user_id: '', completed: '' };

                    this.$options.methods.toggleAlert('Task was added.');
                })
                .error(function(validation){
                    this.$set('validation', validation);
                });
        },
        toggleTaskCompletion: function(task) {
            task.completed = ! task.completed;
            this.$http.post('api/tasks/complete-task/'+ task.id, task);
        },
        editTask: function(task) {
            if(task.completed) return;
            this.editingTask = task;
        },
        cancelEdit: function (todo) {
            this.editingTask = false;
        },
        updateTask: function(task) {
            task.description = task.description.trim();
            this.$http.patch('api/tasks/'+ task.id, task);
            this.$options.methods.toggleAlert('Task was updated.');
            return this.editingTask = false;
        },
        deleteTask: function(due_at,task) {
            if(confirm('Are you sure you want to remove this task?')){
                this.tasks.$remove(task);
                this.$http.delete('api/tasks/'+ task.id, task);
                this.$options.methods.toggleAlert('Task was removed.');
            }
        }
    },

    directives: {
        'task-focus': function (value) {
            if (!value) {
                return;
            }
            var el = this.el;
            Vue.nextTick(function () {
                el.focus();
            });
        }
    }
})
var tasks=新的Vue({
el:'任务',
数据:{
任务:[],
用户:[],
新任务:{description:'',到期日:'',用户id:'',已完成:false},
编辑问:错,
showarert:false,
加载:对,
可见性:“活动”,
验证:[],
showUser:null,
authUser:{}
},
就绪:函数(){
这是.getAuthUser();
this.getTasks();
这是getUsers();
},
计算:{
filteredTasks:函数(){
返回此.options.filters[this.visibility](this.tasks);
},
剩余:函数(){
返回this.tasks.filter(函数(任务){
return!task.completed&&task.user\u id==this.authUser.id;
}).长度;
}
},
过滤器:{
全部:功能(任务){
返回任务;
},
活动:功能(任务){
返回任务。筛选器(函数(任务){
return!task.completed;
});
},
已完成:功能(任务){
返回任务。筛选器(函数(任务){
返回任务。已完成;
});
},
groupByDate:函数(任务){
var result={};
对于(变量i=0;i
尝试使用
任务。$data。
var tasks = new Vue({
    el: '#tasks',

    data: {
        tasks: [],
        users: [],
        newTask: { description: '', due_at: '', user_id: '', completed: false },
        editingTask: false,
        showAlert: false,
        loading: true,
        visibility: 'active',
        validation: [],
        showUser: null,
        authUser: {}
    },

    ready: function() {
        this.getAuthUser();
        this.getTasks();
        this.getUsers();
    },

    computed: {
        filteredTasks: function () {
            return this.$options.filters[this.visibility](this.tasks);
        },
        remaining: function() {
            return this.tasks.filter(function(task){
                return !task.completed && task.user_id == this.authUser.id;
            }).length;
        }
    },

    filters: {
        all: function(tasks) {
            return tasks;
        },
        active: function(tasks) {
            return tasks.filter(function(task){
                return !task.completed;
            });
        },
        completed: function(tasks) {
            return tasks.filter(function(task){
                return task.completed;
            });
        },
        groupByDate: function(tasks) {
            var result = {};
            for (var i = 0; i < tasks.length; i++) {
                var task = tasks[i];

                // Convert due_at date to be used as array key
                var due_at = moment(task.due_at,'YYYY-MM-DD').format('DD-MM-YYYY');

                if (result[due_at]) {
                    result[due_at].push(task);
                } else {
                    result[due_at] = [task];
                }
            }
            return result;
        },
        newDate: function(date) {
            return moment(date, 'DD-MM-YYYY').format('LL');
        },
        usersFilter: function(tasks, user_id) {
            if(this.visibility == 'all' && user_id){
                return tasks.filter(function(task){
                    return task.user_id == user_id;
                });
            }else{
                return tasks;
            }
        }
    },

    methods: {
        getTasks: function () {
            this.loading = true;
            this.$http.get('api/tasks/'+ this.visibility).success(function(tasks) {
                this.$set('tasks', tasks);
                this.loading = false;
            }).error(function(error) {
                console.log(error);
            });
        },
        getUsers: function() {
            this.$http.get('api/users/all',function(users){
                this.$set('users',users);
            });
        },
        getAuthUser: function() {
            this.$http.get('api/users/current-user').success(function(authUser) {
                this.$set('authUser',authUser);
            });
        },
        toggleVisibility: function(filter) {  
            this.visibility = filter;
            this.getTasks();
        },
        open: function(e) {  
            e.preventDefault();
            $('#add-task-modal').slideDown();
        },
        close: function(e) {  
            e.preventDefault();
            $('#add-task-modal').slideUp();
        },
        toggleAlert: function(message) {  
            this.showAlert = true;  
            $('.alert').text(message);
            $('.alert').fadeIn().delay(1000).fadeOut();
            this.showAlert = false; 
        },
        addTask: function(e) {
            e.preventDefault();

            if ( ! this.newTask) return;

            var task = this.newTask;

            this.$http.post('api/tasks', task)
                .success(function(data){
                    task.id = data.task_id;
                    task.due_at = moment(task.due_at,'DD-MM-YYYY').format('YYYY-MM-DD');

                    if(this.visibility == 'all'){
                        this.tasks.push(task);
                    }else if(this.authUser.id == task.user_id){
                        this.tasks.push(task);
                    }

                    $('.datepicker').datepicker('clearDates');

                    this.validation = [];
                    this.newTask = { description: '', due_at: '', user_id: '', completed: '' };

                    this.$options.methods.toggleAlert('Task was added.');
                })
                .error(function(validation){
                    this.$set('validation', validation);
                });
        },
        toggleTaskCompletion: function(task) {
            task.completed = ! task.completed;
            this.$http.post('api/tasks/complete-task/'+ task.id, task);
        },
        editTask: function(task) {
            if(task.completed) return;
            this.editingTask = task;
        },
        cancelEdit: function (todo) {
            this.editingTask = false;
        },
        updateTask: function(task) {
            task.description = task.description.trim();
            this.$http.patch('api/tasks/'+ task.id, task);
            this.$options.methods.toggleAlert('Task was updated.');
            return this.editingTask = false;
        },
        deleteTask: function(due_at,task) {
            if(confirm('Are you sure you want to remove this task?')){
                this.tasks.$remove(task);
                this.$http.delete('api/tasks/'+ task.id, task);
                this.$options.methods.toggleAlert('Task was removed.');
            }
        }
    },

    directives: {
        'task-focus': function (value) {
            if (!value) {
                return;
            }
            var el = this.el;
            Vue.nextTick(function () {
                el.focus();
            });
        }
    }
})