uni-app公用模块
定义一个公用的模块,用来组织和管理这些全局的变量,在需要的页面引入,一般放在根目录下common目录里,然后用的时候在页面中引入该模块,这种方式维护起来比较方便,但是用的时候每次都得引入挂载到Vue.prototype
在main.js中挂载属性/方法
import req from './api/index'
import util from './utils/util'
Vue.prototype.api = 'http://uniapp.dcloud.io'
Vue.prototype.now = Date.now || function () {
return new Date().getTime();
};
Vue.prototype.$api = req
Vue.prototype.$util = util
引用的时候
<script>
export default {
data() {
return {};
},
onLoad(){
console.log('now:' + this.now());
},
methods: {
getProjectWorktime(userId, workDay) {
this.$api.wktime.getWktimeStatus(userId,
{workDay: workDay}).then(res => {
console.log(res)
})
}
}
</script>