Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the window object in vue js?

Tags:

vue.js

vuejs2

I have this vue js component:

<template>
    <div>
hello world
    </div>
</template>

<script>
  export default {
    name: 'mycomp',
    data: function () {
      console.error("window.google_recaptcha_public_key", window.google_recaptcha_public_key);
      return {

      }
    },
    mounted() {
      let app = this;
      console.error("window.google_recaptcha_public_key2", window.google_recaptcha_public_key);

    },
  }
</script>

<style scoped lang="scss">
</style>

returns:

window.google_recaptcha_public_key undefined 
window.google_recaptcha_public_key2 undefined

where can I leave painless and happy all global configuration?

notice this configuration lives in my laravel backend. So I wont copy paste all values from the backend to the front end

like image 262
Toskan Avatar asked Jan 13 '19 07:01

Toskan


People also ask

How do I access Vue data property?

import FiDis from '../components/fi_dis. vue' Vue. component('fi_dis', FiDis); document. addEventListener('turbolinks:load', () => { const fi_dis = new Vue({ el: '#bs', components: { FiDis }, props['bId'], created() { // This is a lifecycle method this.

How do I access my Vue filter?

All you need to do is call the vm. $options object wherever you need to use your previously created filters. That's it! You can now apply your filters inside the Vue instance.

How do I view Vue files in my browser?

Open in browser To view the project, open a tab and type http://localhost:3000 into the URL bar. That's the address that the Node server is listening to. You should now see this page, which is the basic structure of our project.

Can I use getElementById in Vue?

To use document. getElementById in Vue. js, we can assign a ref to the element we want to get. And then we can use this.


3 Answers

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: '<App/>',
    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: '<App/>',
    components: {App}
});

inside application

mounted() {
  console.log(this.GoogleRecaptcha)
}
like image 103
Moumen Soliman Avatar answered Oct 09 '22 10:10

Moumen Soliman


You should save your window variable in Vue.prototype

main.js

Vue.prototype.$authUser = window.authUser;

After that, you can access your data as follows:

Vue template

<div v-text="$authUser.name"></div>

Vue script

let name = this.$authUser.name;
like image 41
Ilya Degtyarenko Avatar answered Oct 09 '22 10:10

Ilya Degtyarenko


In Vue3, you no longer have the global Vue instance, so you need to assign the window as a global property on your app...

// main.js
app.config.globalProperties.window = window

Then in your components, window will just work.

This info is from an impeccable source.

like image 43
bbsimonbb Avatar answered Oct 09 '22 10:10

bbsimonbb