将全局变量应用于 Vuejs [英] Apply global variable to Vuejs

查看:30
本文介绍了将全局变量应用于 Vuejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 javascript 变量,我想在实例化时将其全局传递给 Vue 组件,因此每个注册的组件都将其作为属性或可以全局访问.

I have a javascript variable which I want to pass globally to Vue components upon instantiation thus either each registered component has it as a property or it can be accessed globally.

注意:: 我需要将 vuejs 的这个全局变量设置为 READ ONLY 属性

Note:: I need to set this global variable for vuejs as a READ ONLY property

推荐答案

只需 添加实例属性

比如所有组件都可以访问一个全局的appName,你只需要写一行代码:

For example, all components can access a global appName, you just write one line code:

Vue.prototype.$appName = '我的应用'

$ 不是魔术,它是 Vue 用于所有实例的属性的约定.

$ isn't magic, it's a convention Vue uses for properties that are available to all instances.

或者,您可以编写包含所有全局方法或属性的插件.

这篇关于将全局变量应用于 Vuejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆