Home > Article > Web Front-end > How to define global variables and global methods in vue? (code)
This article introduces to you how to define global variables and global methods in vue? (code), it has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Global import file
1. First define the common component common.vue
2. Import it where you need to use it
Use
export default { data () { return { username: '', password: '', globalHttpUrl: global.httpUrl, promiseFun: global.promiseFun } }, methods: { loginInFun () { localStorage.setItem('userId', '00001') let params = { telphone: this.username, password: this.password } this.promiseFun('itArtison/user/login', params).then( (res) => { console.log(res) this.$Message.info(res.message) // 登录成功过以后,这里从初session // 先将对象转换为json字符串 localStorage.setItem('userInfo', JSON.stringify(res.data)) if (res.code === '0000') { this.$router.push({'name': 'Home'}) } }, (err) => { console.log(err) this.$Message.info(err.message) } ) } }
Recommended related articles:
Detailed explanation of global variables and global functions defined in the vue project
About Detailed explanation of several methods of defining global variables in VUE
The above is the detailed content of How to define global variables and global methods in vue? (code). For more information, please follow other related articles on the PHP Chinese website!