How to access the window object in vue js?

前端 未结 2 570
夕颜
夕颜 2021-02-05 07:12

I have this vue js component:





        
2条回答
  •  一向
    一向 (楼主)
    2021-02-05 07:56

    U can use Vue.prototype in main.js file, or in file you import Vue

    Vue.prototype.Hereanyname = window.hereanyname;
    

    and in your Vue application, you can use it

    Hereanyname.thefunction
    

    Real example on Laravel

    in main.js

    import Vue from 'vue';
    Vue.prototype.Routes = window.routes;
    
    new Vue({
        el: '#app',
        template: '',
        components: {App}
    });
    

    in your application

    :href="Routes.route('laravel.route.here')"
    

    So for your case

    import Vue from 'vue';
    Vue.prototype.GoogleRecaptcha = window.google_recaptcha_public_key;
    
    new Vue({
        el: '#app',
        template: '',
        components: {App}
    });
    

    inside application

    mounted() {
      console.log(this.GoogleRecaptcha)
    }
    

提交回复
热议问题