项目引入了uview,并将uview所有模块指给uniapp全局变量uni

  uni.$u=$u

 在登录页面,或者APP.vue打印以下变量:

this, uni, $u, vm, uni.$u, this.$u

 //  this,$u,vm,uni,  this.$u, uni.$u全局变量说明
    console.log(">>this", this)
    console.log(">>uni", uni)
    console.log(">>this===uni", this === uni) //false
    console.log(">>this.$u ", this.$u)
    console.log(">>uni.$u ", uni.$u)
    console.log(">>this.$u===uni.&u", this.$u === uni.$u) //ture

    try {//$u is not defined
      console.log(">>$u ", $u)
    } catch (e) {
      console.log(e)
    }
    try { //vm is not defined
      console.log(">>this.vm", this.vm)
      console.log(">>vm ", vm)
    } catch (e) {
      console.log(e)
    }

运行结果

$u    是uview挂载到uni上的,方便使用uni来操作uview组件。 

uni.  对象是uni-app框架实例。

this.  在.vue页面,对应就是当前vue的实例对象。

this.$u 与uni.$u    在vue页面是同一对象,指向 uview

$u来源

uniapp-master\uview-ui\index.vue,在此vue里,通过xx.prototype.$u的方法 ,通过uni.$u方法同时挂载到全局里。

// 引入全局mixin
import mixin from './libs/mixin/mixin.js'


// 防抖方法
import debounce from './libs/function/debounce.js'
// 节流方法
import throttle from './libs/function/throttle.js'
import {fixMoney2Decimal, formatInputMoney, toFixed2Decimal} from "@/uview-ui/libs/function/numberform";
import {positiveNumber} from "@/uview-ui/libs/function/inputRegExp";
//...
//...

// 配置信息
import config from './libs/config/config.js'
// 各个需要fixed的地方的z-index配置文件
import zIndex from './libs/config/zIndex.js'
import {timeFormatCmm} from "@/uview-ui/libs/function/timeFormatStr";

const $u = {
  queryParams: queryParams,
  positiveNumber: positiveNumber,
  route: route,
  timeFormat: timeFormat,
  date: timeFormat, // 另名date
  timeFormatCmm: timeFormatCmm,
  timeFrom,
  colorGradient: colorGradient.colorGradient,
  colorToRgba: colorGradient.colorToRgba,
  guid,
  color,
  sys,
  os,
  type2icon,
  randomArray,
  wranning,
  get: http.get,
  post: http.post,
  put: http.put,
  'delete': http.delete,
  hexToRgb: colorGradient.hexToRgb,
  rgbToHex: colorGradient.rgbToHex,
  test,
  random,
  deepClone,
  deepMerge,
  getParent,
  $parent,
  addUnit,
  trim,
  type: ['primary', 'success', 'error', 'warning', 'info'],
  http,
  toast,
  toastErr,
  toastS,
  modal,
  config, // uView配置信息相关,比如版本号
  zIndex,
  debounce,
  throttle,

  //--
  toFixed2Decimal,
  fixMoney2Decimal,
  formatInputMoney
}

// $u挂载到uni对象上
uni.$u = $u

const install = Vue => {
  Vue.mixin(mixin)
  if (Vue.prototype.openShare) {
    Vue.mixin(mpShare);
  }
  // Vue.mixin(vuexStore);
  // 时间格式化,同时两个名称,date和timeFormat
  Vue.filter('timeFormat', (timestamp, format) => {
    return timeFormat(timestamp, format)
  })
  Vue.filter('date', (timestamp, format) => {
    return timeFormat(timestamp, format)
  })
  // 将多久以前的方法,注入到全局过滤器
  Vue.filter('timeFrom', (timestamp, format) => {
    return timeFrom(timestamp, format)
  })
  Vue.prototype.$u = $u
}

export default {
  install
}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐