How to define global variables using vue.js
How to use vue.js to define global variables: First, create a separate global variable module file, and define some initial states of variables in the module; then introduce it in [main.js] and pass it through [Vue.prototype] Mounted to the vue instance.
The operating environment of this tutorial: Windows 7 system, Vue version 2.9.6, DELL G3 computer.
【Recommended related articles: vue.js】
How to use vue.js to define global variables:
Principle:
1. Create a new global variable module file separately, define some initial states of variables in the module, and use export default to expose them.
2. Introduce it in main.js and mount it to the vue instance through Vue.prototype
. For use by other module files;
3. Or directly introduced into the required module file for use;
Step 1. Define a global component Global.vue, which only contains <script> ;
<!--设置全局变量--> <script> //接口地址 const BASE_URL = 'http://118.189.105.152:7181/qianzhang-transf/index/'; //定义全局接口地址 //请求头部 const reqHead = { "transDate": "20180816", "encryptFlag": "1", "seqNo": "2018081628507127", "serviceID": "loanLmtQryHXMKL", "transTime": "174341", "channelID": "netbank" } export default { //将常量暴露出去 BASE_URL, reqHead, } </script>
Step 2. Modify the prototype chain
// The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' import router from './router' import fastclick from 'fastclick' import global_ from './components/Global.vue' //引入全局组件 Vue.prototype.GLOBAL = global_; //修改原型 Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', router, components: { App }, template: '<App/>' })
Step 3. Use
directly on the required vue pagethis.GLOBAL.BASE_URL
, you can get the defined constant value
Related free learning recommendations:javascript(Video)
The above is the detailed content of How to define global variables using vue.js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When using the Vue framework to develop front-end projects, we will deploy multiple environments when deploying. Often the interface domain names called by development, testing and online environments are different. How can we make the distinction? That is using environment variables and patterns.

The difference between C++ local variables and global variables: Visibility: Local variables are limited to the defining function, while global variables are visible throughout the program. Memory allocation: local variables are allocated on the stack, while global variables are allocated in the global data area. Scope: Local variables are within a function, while global variables are throughout the program. Initialization: Local variables are initialized when a function is called, while global variables are initialized when the program starts. Recreation: Local variables are recreated on every function call, while global variables are created only when the program starts.

Ace is an embeddable code editor written in JavaScript. It matches the functionality and performance of native editors like Sublime, Vim, and TextMate. It can be easily embedded into any web page and JavaScript application. Ace is maintained as the main editor for the Cloud9 IDE and is the successor to the Mozilla Skywriter (Bespin) project.

The difference between componentization and modularization: Modularization is divided from the perspective of code logic; it facilitates code layered development and ensures that the functions of each functional module are consistent. Componentization is planning from the perspective of UI interface; componentization of the front end facilitates the reuse of UI components.

Foreword: In the development of vue3, reactive provides a method to implement responsive data. This is a frequently used API in daily development. In this article, the author will explore its internal operating mechanism.

Vue.js has become a very popular framework in front-end development today. As Vue.js continues to evolve, unit testing is becoming more and more important. Today we’ll explore how to write unit tests in Vue.js 3 and provide some best practices and common problems and solutions.

In Vue.js, developers can use two different syntaxes to create user interfaces: JSX syntax and template syntax. Both syntaxes have their own advantages and disadvantages. Let’s discuss their differences, advantages and disadvantages.

How to handle exceptions in Vue3 dynamic components? The following article will talk about Vue3 dynamic component exception handling methods. I hope it will be helpful to everyone!
