Как добавить новое свойство в данные vue (объект)? - PullRequest
1 голос
/ 12 января 2020

Я получаю следующую ошибку при использовании нумерации страниц в vue. js

vue. js: 2192 Uncaught (в обещании) TypeError: this.categories.pu sh не является функцией в VueComponent.getAllChildren (vue. js: 2192) в vue. js: 2207

script:

export default {
    name: "DisplayCategories",
    data() {
        return {
            categories: {},
        }
    },
    mounted() {
        this.categoriesList();
    },
    methods: {
        getAllChildren(currentValue, level) {

            for (let i = 0; i < currentValue.length; i++) {
                let current = currentValue[i];
                this.categories.push({
                    id: current.id,
                    name: Array(level + 1).join("....") + " " + current.name
                })
                if (current.children_recursive && current.children_recursive.length > 0) {
                    this.getAllChildren(current.children_recursive, level + 1)
                }
            }
        },
        categoriesList() {
            let uri = '/api/categories';
            this.axios.get(uri).then(response => {
                this.getAllChildren(response.data.data, 0)
            });
        },
        getResults(page = 1) {
            axios.get('/api/categories?page=' + page)
                .then(response => {
                    this.categories = response.data;
                });
        }
    }
}

контроллер:

public function index()
{
    $categories = Category::with('childrenRecursive')
        ->where('parent_id', null)
        ->paginate(5);

    return response()->json($categories, 200);
}

1 Ответ

1 голос
/ 12 января 2020

Вы определили categories как объект, а метод .push() можно использовать только для массивов. Определите categories как массив:

    export default {
            name: "DisplayCategories",
            data(){
                return{
                    categories:[],

                }
            }, 
...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...