如何访问vue js中的window对象? [英] How to access the window object in vue js?
问题描述
我有这个 vue js 组件:
<div>你好,世界
</模板><脚本>导出默认{name: 'mycomp',数据:函数(){console.error("window.google_recaptcha_public_key", window.google_recaptcha_public_key);返回 {}},安装(){让应用程序=这个;console.error("window.google_recaptcha_public_key2", window.google_recaptcha_public_key);},}<style scoped lang="scss"></风格>
返回:
window.google_recaptcha_public_key 未定义window.google_recaptcha_public_key2 未定义
我在哪里可以轻松愉快地完成所有全局配置?
注意这个配置存在于我的 Laravel 后端.所以我不会将所有值从后端复制粘贴到前端
你可以在 main.js
文件或 文件中使用
Vue.prototype
导入Vue
Vue.prototype.Hereanyname = window.hereanyname;
并且在您的 Vue 应用程序中,您可以使用它
Hereanyname.thefunction
Laravel
在main.js
从'vue'导入Vue;Vue.prototype.Routes = window.routes;新的 Vue({el: '#app',模板:'<App/>',组件:{应用}});
在您的应用程序中
:href="Routes.route('laravel.route.here')"
所以对于你的情况
从'vue'导入Vue;Vue.prototype.GoogleRecaptcha = window.google_recaptcha_public_key;新的 Vue({el: '#app',模板:'<App/>',组件:{应用}});
内部应用
mounted() {console.log(this.GoogleRecaptcha)}
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
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)
}
这篇关于如何访问vue js中的window对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!