原生小程序文件导入

187 阅读1分钟

文件导入

  • 在wxml中引入wxml:<import src='路径'>;
  • 在js中引入js:let 名字 = require('相对路径');
  • wxss中引入wxss: @import '路径';

更新数据(setData)

data: {
    userInfo:[],//用户信息
  },
  
this.setData({
    userInfo:[name:"hahah"]
})

获取当前点击数据

  1. 在标签中定义data-属性
  2. 在绑定事件处理通过e.currentTarget.dataset获取 例如:在数据渲染时
<!-- wxml  设置"data-type"    -->
<block wx:for-items="{{List}}" wx:key="{{index}}">
<text bindtap="handle" data-type="{{item.id}}">{{item}}</text>
</block>
//通过e.currentTarget.dataset获取type
handle(e){
    console.log(e.currentTarget.dataset.type)
}

动态设置json配置

1 设置页面导航条颜色

//设置页面导航条颜色
wx.setNavigationBarColor({
  frontColor: '#ffffff',
  backgroundColor: '#ff0000',
  animation: {
    duration: 400,
    timingFunc: 'easeIn'
  }
})

2 动态设置当前页面的标题

wx.setNavigationBarTitle({
  title: '当前页面'
})

3 动态设置下拉背景字体、loading 图的样式

wx.setBackgroundTextStyle({
  textStyle: 'dark' // 下拉背景字体、loading 图的样式为dark
})

4 动态设置窗口的背景色

wx.setBackgroundColor({
  backgroundColor: '#ffffff', // 窗口的背景色为白色
})

wx.setBackgroundColor({
  backgroundColorTop: '#ffffff', // 顶部窗口的背景色为白色
  backgroundColorBottom: '#ffffff', // 底部窗口的背景色为白色
})

....

获取app.js里的全局属性globalData

在其他页面page中

在其他页面page中,修改globalData的方法是

ar app = getApp();
console.log(app.globalData)

在app.js中

而在app.js中需要注意的是,应当在小程序初始化完成以后再更改全局变量的值,即在onLaunch函数中:

onLaunch: function () {
    var that = this;
    console.log('App Launch');
    that.globalData.theme = "block";
  },

一定要用that去设置,定义that也要在onLaunch函数一开始就定义,因为this是默认指向最靠近的那一层。