如何从Vue.extend调用$ get?

时间:2016-02-25 08:41:10

标签: vue.js

示例未显示如何从Vue.extend调用此类函数。现在我收到错误: ReferenceError: $get is not defined

var guestContent = Vue.extend({
      template: `

        <p>Guest content</p>
      <div v-for="question in questions">
       <template v-if="question.isEnabled">
          <h3 v-if="question.isEnabled">{{question.question}}</h3>

          <!-- First Level -->
            <div v-for="firstLevelAnswer in question.answers">                
              <label v-if="!question.isRadioButton"><span class="firstLevelAnswer"><input type="checkbox" class="big-checkbox" v-on:onclick="setIsSelectedTrue()"/>{{firstLevelAnswer.answer}}</span></label>
              <label v-if="question.isRadioButton"><span class="firstLevelAnswer"><input type="radio" class="big-checkbox" name="myvalue"/>{{firstLevelAnswer.answer}}</span></label>
              <span v-if="firstLevelAnswer.isTextInput"><input type="text"/></span>
                   |  firstLevelAnswer.isSelected: {{firstLevelAnswer.isSelected}}  

            </div>
         </template>
      </div>  

    </li>

          `,
        data: function ()  {
          return {
             questions: []
          }

          },
          ready() 
          { 
            this.getQuestionsContent(),
            this.foo()
          },
          methods: 
          {

             getQuestionsContent()
             {
                this.$http.get('http://127.0.0.1:8080/js/questions.json').then(function(response)
                {
                  this.questions = response.data;

                }); 

              },
              foo()
              {
                console.log($get('questions.question')); //HERE IS PROBLEM
              }

          }

        }
        );

我还可以查看来自foo的值firstLevelAnswer.isSelected吗?我该怎么办?它是在for循环中生成的,并不喜欢它可以存在于此处......

1 个答案:

答案 0 :(得分:1)

$ get是Vue组件的实例方法,因此您必须使用this.$get调用它:

foo()
  {
    console.log(this.$get('questions.question'));
  }