Home  >  Article  >  Web Front-end  >  How to define global variables and global methods in vue? (code)

How to define global variables and global methods in vue? (code)

不言
不言Original
2018-08-01 17:04:506708browse

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:

How to mount the vue component globally? Introduction to the method of mounting Vue components globally (code)

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn