Vue:组件渲染时数据“未定义”

时间:2018-08-10 04:31:52

标签: vue.js

当当前语言等于元素ID时,我想将一个类设置为我的元素之一。但是当解雇method进行检查时,我的日志为“未定义”。

为什么?如何正确设置该类?

是否先加载数据对象然后再渲染组件?

<template>
   <div>
       <div class="star-box">
            <div class="head">
                <img :src="'images/flags/en.png'"
                     id="en"
                     class="student-img"
                     v-bind:class="{'activeLanguage': checkActiveLanguage('en')}"
                     alt=""
                   >
                </div>
                <div class="body">
                     <h5 class="heading">English</h5>
                </div>
         </div> 
         <div class="star-box">
              <div class="head">
                    <img :src="'images/flags/de.png'"
                        id="de"
                        class="student-img"
                        v-bind:class="{'activeLanguage': checkActiveLanguage('de')}"
                        alt=""
                     >
               </div>
               <div class="body">
                     <h5 class="heading">Deutsch</h5>
                </div>
         </div>
   </div>
</template>

JS

<script>
    import {mapActions,mapGetters} from 'vuex';

    export default {
        name: 'Language',
        data() {
            return {
                language: ''
            }
        },
        methods: {
            checkActiveLanguage: (lang)=> {
                console.log(this.language);
                if(lang==this.language) return true;
            },
            ...mapGetters(['getCurrentLanguage']),

        },
        beforeMount(){
            this.language =  this.getCurrentLanguage();
        }
    }
</script>

0 个答案:

没有答案