在js文件中引入vue实例

应用场景:

一般情况下,请求数据wx.request或者axios 请求数据,会新建一个api.js,进行封装。

举例:

 wx.request(
     {
      header: {
        'content-type':'application/x-www-form-urlencoded'
      },
      url: baseUrl + config.url,
      method:config.method,
      data:{
        token:Vue.$store.state.loginUserInfo["userId"],
        ...config.params
      },
      success:(res)=>{ 
        if(res.data.code==116){
          wx.reLaunch({ url: "../logs/main" });
        }else if(res.data.code==0){
          return resolve(res.data)
        }else{
            wx.showToast({
                             title: res.data.msg,
                              icon: 'none'
                                })
        }
        
      },
      fail:(err)=>{return reject(err)}
    })
View Code

这个情况下,需要获取到vue实例store上的数据,所以需要在js文件中,引入vue实例

 

1.导出vue实例 在main.js中

var vueDom=new Vue({
    store,
}).$mount('#app')
export default vueDom

2.api.js(需要用vue实例的js)引入vue实例

import Vue from '../main'

3.使用

Vue.$store.state.loginUserInfo["userId"]

 

posted @ 2021-04-22 18:14  明媚下雨天  阅读(1702)  评论(0编辑  收藏  举报