最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Populate Vue data object, and console log results - Stack Overflow

programmeradmin3浏览0评论

I have a huge list of data in a Vue ponent that I would like to dynamically populate. I'm trying to do that by a loop, and a global variable. It looks like below:

    var filterItems = document.querySelectorAll('.filter-title');
    var filterItemsTitle = filterItems.innerHTML;
    var arr = [];

    function buildContent() {
      for (var i = 0; i < filterItems.length; i++) {
        arr.push(filterItems[i].innerHTML);
      }
      console.log(arr)
    }

    window.onload = buildContent

    var titleFilter = new Vue({
      // in items its refering to arr built in buildContent
      el: '#filter',
      data: {
         // arr es from buildContent() - I think...does this work?
         items: arr,
         filters: [
            { label: 'All Titles', value: false },
            { label: 'a', value: 'a' },
            { label: 'b', value: 'b' },
            { label: 'c', value: 'c' },
        ],
        currentFilter: false,
      },
      methods: {
        filterItems: function(e){
          console.log(items)
        }
      }
    });

Basically what I want to do is take the arr variable which is populated with an array of content in the buildContent() loop and reference it in the items data section of my vue ponent

  1. is this possible? Or no?
  2. How can I console.log() the items object? When I try to do it it is undefined

I have a huge list of data in a Vue ponent that I would like to dynamically populate. I'm trying to do that by a loop, and a global variable. It looks like below:

    var filterItems = document.querySelectorAll('.filter-title');
    var filterItemsTitle = filterItems.innerHTML;
    var arr = [];

    function buildContent() {
      for (var i = 0; i < filterItems.length; i++) {
        arr.push(filterItems[i].innerHTML);
      }
      console.log(arr)
    }

    window.onload = buildContent

    var titleFilter = new Vue({
      // in items its refering to arr built in buildContent
      el: '#filter',
      data: {
         // arr es from buildContent() - I think...does this work?
         items: arr,
         filters: [
            { label: 'All Titles', value: false },
            { label: 'a', value: 'a' },
            { label: 'b', value: 'b' },
            { label: 'c', value: 'c' },
        ],
        currentFilter: false,
      },
      methods: {
        filterItems: function(e){
          console.log(items)
        }
      }
    });

Basically what I want to do is take the arr variable which is populated with an array of content in the buildContent() loop and reference it in the items data section of my vue ponent

  1. is this possible? Or no?
  2. How can I console.log() the items object? When I try to do it it is undefined
Share Improve this question asked Jan 5, 2018 at 21:42 kawnahkawnah 3,4148 gold badges63 silver badges116 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

Try changing filterItems to:

filterItems: function(e){
      console.log(JSON.stringify(this.items))
    }

From looking at the code you provided, that should work.

发布评论

评论列表(0)

  1. 暂无评论