前端技术前沿6

不念不忘少年蓝@ 2021-09-19 13:06 439阅读 0赞

数据缓存setStorageSync和getStorage

  1. //获取输入值
  2. getInput: function(e) {
  3. var self = this;
  4. self.setData({
  5. storage: e.detail.value
  6. })
  7. },
  8. //存储输入值
  9. saveInput: function() {
  10. var self = this;
  11. wx.setStorageSync('storage', self.data.storage)
  12. },
  13. output: function () {
  14. var self = this;
  15. wx.getStorage({
  16. key: 'storage',
  17. success: function (res) {
  18. // success
  19. self.setData({
  20. storage2: res.data
  21. })
  22. }
  23. })
  24. }

数组

  1. <view wx:for="{
  2. {[zero, 1, 2, 3, 4]}}">{
  3. {item}}</view>
  4. Page({
  5. data: {
  6. zero: 0
  7. }
  8. })

最终组合成数组[0, 1, 2, 3, 4]。

对象

  1. <template is="objectCombine" data="{
  2. {for: a, bar: b}}"></template>
  3. Page({
  4. data: {
  5. a: 1,
  6. b: 2
  7. }
  8. })

最终组合成的对象是 {for: 1, bar: 2}

扩展运算符 … 来将一个对象展开

  1. <template is="objectCombine" data="{
  2. {...obj1, ...obj2, e: 5}}"></template>
  3. Page({
  4. data: {
  5. obj1: {
  6. a: 1,
  7. b: 2
  8. },
  9. obj2: {
  10. c: 3,
  11. d: 4
  12. }
  13. }
  14. })

最终组合成的对象是 {a: 1, b: 2, c: 3, d: 4, e: 5}。

对象的 key 和 value 相同,也可以间接地表达。

  1. <template is="objectCombine" data="{
  2. {foo, bar}}"></template>
  3. Page({
  4. data: {
  5. foo: 'my-foo',
  6. bar: 'my-bar'
  7. }
  8. })

最终组合成的对象是 {foo: ‘my-foo’, bar:‘my-bar’}。

微信小程序-消息提示框

  1. wx.showToast(OBJECT)
  2. wx.showToast({
  3. title:'成功',
  4. icon:'success',
  5. duration: 2000
  6. })
  7. wx.showToast({
  8. title:'加载中',
  9. icon:'loading',
  10. duration: 10000
  11. })
  12. setTimeout(function(){
  13. wx.hideToast()
  14. },2000)
  15. wx.showModal({
  16. title:'提示',
  17. content:'这是一个模态弹窗',
  18. success:function(res) {
  19. if(res.confirm) {
  20. console.log('用户点击确定')
  21. }
  22. }
  23. })

wx.showActionSheet(OBJECT)

显示操作菜单

  1. wx.showActionSheet({
  2. itemList: ['A','B', 'C'],
  3. success:function(res) {
  4. if(!res.cancel) {
  5. console.log(res.tapIndex)
  6. }
  7. }
  8. })

wx.setNavigationBarTitle(OBJECT)

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

wx.showNavigationBarLoading()

在当前页面显示导航条加载动画。

wx.hideNavigationBarLoading()

隐藏导航条加载动画。

页面跳转:

wx.navigateTo(OBJECT)

  1. wx.navigateTo({
  2. url:'test?id=1'
  3. })

wx.navigateBack(OBJECT)

关闭当前页面,返回上一页面或多级页面。

  1. var animation = wx.createAnimation({
  2. transformOrigin:"50% 50%",
  3. duration: 1000,
  4. timingFunction:"ease",
  5. delay: 0
  6. })

wx.hideKeyboard()

收起键盘

wx.stopPullDownRefresh()

停止当前页面下拉刷新

image.png

API
基础
image.png

image.png

image.png

image.png

image

  1. /**
  2. project
  3. └── src
  4. ├── components
  5. | └── child.wpy
  6. ├── pages
  7. | ├── index.wpy index 页面配置、结构、样式、逻辑
  8. | └── log.wpy log 页面配置、结构、样式、逻辑
  9. └──app.wpy 小程序配置项(全局样式配置、声明钩子等)
  10. **/
  11. // index.wpy
  12. <template>
  13. <!-- 注意,使用for属性,而不是使用wx:for属性 -->
  14. <repeat for="{
  15. {list}}" key="index" index="index" item="item">
  16. <!-- 插入<script>脚本部分所声明的child组件,同时传入item -->
  17. <child :item="item"></child>
  18. </repeat>
  19. </template>
  20. <script>
  21. import wepy from 'wepy';
  22. // 引入child组件文件
  23. import Child from '../components/child';
  24. export default class Index extends wepy.page {
  25. components = {
  26. // 声明页面中要使用到的Child组件的ID为child
  27. child: Child
  28. }
  29. data = {
  30. list: [{id: 1, title: 'title1'}, {id: 2, title: 'title2'}]
  31. }
  32. }
  33. </script>
  34. <template>
  35. <view class="child1">
  36. <child></child>
  37. </view>
  38. <view class="child2">
  39. <anotherchild></anotherchild>
  40. </view>
  41. </template>
  42. <script>
  43. import wepy from 'wepy';
  44. import Child from '../components/child';
  45. export default class Index extends wepy.page {
  46. components = {
  47. //为两个相同组件的不同实例分配不同的组件ID,从而避免数据同步变化的问题
  48. child: Child,
  49. anotherchild: Child
  50. };
  51. }
  52. </script>
  53. /**
  54. project
  55. └── src
  56. ├── components
  57. | └── child.wpy
  58. ├── pages
  59. | ├── index.wpy index 页面配置、结构、样式、逻辑
  60. | └── log.wpy log 页面配置、结构、样式、逻辑
  61. └──app.wpy 小程序配置项(全局公共配置、公共样式、声明钩子等)
  62. **/
  63. // index.wpy
  64. <template>
  65. <!-- `<script>`脚本部分中所声明的组件ID为名命名自定义标签,从而在`<template>`模板部分中插入组件 -->
  66. <child></child>
  67. </template>
  68. <script>
  69. import wepy from 'wepy';
  70. //引入组件文件
  71. import Child from '../components/child';
  72. export default class Index extends wepy.page {
  73. //声明组件,分配组件id为child
  74. components = {
  75. child: Child
  76. };
  77. }
  78. </script>

image

  1. // 错误示例
  2. import wepy from 'wepy';
  3. export default class MyComponent extends wepy.component {
  4. methods = {
  5. bindtap () {
  6. let rst = this.commonFunc();
  7. // doSomething
  8. },
  9. bindinput () {
  10. let rst = this.commonFunc();
  11. // doSomething
  12. },
  13. //错误:普通自定义方法不能放在methods对象中
  14. customFunction () {
  15. return 'sth.';
  16. }
  17. };
  18. }
  19. // 正确示例
  20. import wepy from 'wepy';
  21. export default class MyComponent extends wepy.component {
  22. methods = {
  23. bindtap () {
  24. let rst = this.commonFunc();
  25. // doSomething
  26. },
  27. bindinput () {
  28. let rst = this.commonFunc();
  29. // doSomething
  30. },
  31. }
  32. //正确:普通自定义方法在methods对象外声明,与methods平级
  33. customFunction () {
  34. return 'sth.';
  35. }
  36. }
  37. import wepy from 'wepy';
  38. export default class MyPage extends wepy.page {
  39. // export default class MyComponent extends wepy.component {
  40. customData = {} // 自定义数据
  41. customFunction () {} //自定义方法
  42. onLoad () {} // 在Page和Component共用的生命周期函数
  43. onShow () {} // 只在Page中存在的页面生命周期函数
  44. config = {}; // 只在Page实例中存在的配置数据,对应于原生的page.json文件
  45. data = {}; // 页面所需数据均需在这里声明,可用于模板数据绑定
  46. components = {}; // 声明页面中所引用的组件,或声明组件中所引用的子组件
  47. mixins = []; // 声明页面所引用的Mixin实例
  48. computed = {}; // 声明计算属性(详见后文介绍)
  49. watch = {}; // 声明数据watcher(详见后文介绍)
  50. methods = {}; // 声明页面wxml中标签的事件处理函数。注意,此处只用于声明页面wxml中标签的bind、catch事件,自定义方法需以自定义方法的方式声明
  51. events = {}; // 声明组件之间的事件处理函数
  52. }
  53. import wepy from 'wepy';
  54. export default class MyAPP extends wepy.app {
  55. customData = {};
  56. customFunction () { }
  57. onLaunch () {}
  58. onShow () {}
  59. config = {} // 对应 app.json 文件
  60. globalData = {}
  61. }

小程序被分为三个实例:小程序实例App、页面实例Page、组件实例Component

  1. import wepy from 'wepy';
  2. // 声明一个App小程序实例
  3. export default class MyAPP extends wepy.app {
  4. }
  5. // 声明一个Page页面实例
  6. export default class IndexPage extends wepy.page {
  7. }
  8. // 声明一个Component组件实例
  9. export default class MyComponent extends wepy.component {
  10. }
  11. <template lang="wxml">
  12. <view> </view>
  13. </template>
  14. <script>
  15. import wepy from 'wepy';
  16. export default class Com extends wepy.component {
  17. components = {};
  18. data = {};
  19. methods = {};
  20. events = {};
  21. // Other properties
  22. }
  23. </script>
  24. <style lang="less">
  25. /** less **/
  26. </style>
  27. <script>
  28. import wepy from 'wepy';
  29. import Counter from '../components/counter';
  30. export default class Page extends wepy.page {
  31. config = {};
  32. components = {counter1: Counter};
  33. data = {};
  34. methods = {};
  35. events = {};
  36. onLoad() {};
  37. // Other properties
  38. }
  39. </script>
  40. <template lang="wxml">
  41. <view>
  42. </view>
  43. <counter1></counter1>
  44. </template>
  45. <style lang="less">
  46. /** less **/
  47. </style>
  48. <script>
  49. import wepy from 'wepy';
  50. export default class extends wepy.app {
  51. config = {
  52. "pages":[
  53. "pages/index/index"
  54. ],
  55. "window":{
  56. "backgroundTextStyle": "light",
  57. "navigationBarBackgroundColor": "#fff",
  58. "navigationBarTitleText": "WeChat",
  59. "navigationBarTextStyle": "black"
  60. }
  61. };
  62. onLaunch() {
  63. console.log(this);
  64. }
  65. }
  66. </script>
  67. <style lang="less">
  68. /** less **/
  69. </style>
  70. <style lang="less" src="page1.less"></style>
  71. <template lang="wxml" src="page1.wxml"></template>
  72. <script>
  73. // some code
  74. </script>

5 small

wepy.config.js配置文件说明

  1. let prod = process.env.NODE_ENV === 'production';
  2. module.exports = {
  3. 'target': 'dist',
  4. 'source': 'src',
  5. 'wpyExt': '.wpy',
  6. 'compilers': {
  7. less: {
  8. 'compress': true
  9. },
  10. /*sass: {
  11. 'outputStyle': 'compressed'
  12. },
  13. postcss: {
  14. plugins: [
  15. cssnext({
  16. browsers:['iOS 9', 'Android 4.4']
  17. })
  18. ]
  19. },*/
  20. babel: {
  21. 'presets': [
  22. 'es2015',
  23. 'stage-1'
  24. ],
  25. 'plugins': [
  26. 'transform-export-extensions',
  27. 'syntax-export-extensions',
  28. 'transform-runtime'
  29. ]
  30. }
  31. },
  32. 'plugins': {
  33. }
  34. };
  35. if (prod) {
  36. // 压缩sass
  37. module.exports.compilers['sass'] = {'outputStyle': 'compressed'};
  38. // 压缩less
  39. module.exports.compilers['less'] = {'compress': true};
  40. // 压缩js
  41. module.exports.plugins = {
  42. 'uglifyjs': {
  43. filter: /\.js$/,
  44. config: {
  45. }
  46. },
  47. 'imagemin': {
  48. filter: /\.(jpg|png|jpeg)$/,
  49. config: {
  50. 'jpg': {
  51. quality: 80
  52. },
  53. 'png': {
  54. quality: 80
  55. }
  56. }
  57. }
  58. };
  59. }
  60. import wepy from 'wepy';
  61. async onLoad() {
  62. await wepy.login();
  63. this.userInfo = await wepy.getUserInfo();
  64. }
  65. onLoad = function () {
  66. var self = this;
  67. wx.login({
  68. success: function (data) {
  69. wx.getUserInfo({
  70. success: function (userinfo) {
  71. self.setData({userInfo: userinfo});
  72. }
  73. });
  74. }
  75. });
  76. }
  77. 示例代码:
  78. import wepy from 'wepy';
  79. export default class Index extends wepy.page {
  80. getData() {
  81. return new Promise((resolve, reject) => {
  82. setTimeout(() => {
  83. resolve({data: 123});
  84. }, 3000);
  85. });
  86. };
  87. async onLoad() {
  88. let data = await this.getData();
  89. console.log(data.data);
  90. };
  91. }
  92. // index.wpy
  93. <template>
  94. <view>
  95. <panel>
  96. <h1 slot="title"></h1>
  97. </panel>
  98. <counter1 :num="myNum"></counter1>
  99. <counter2 :num.sync="syncNum"></counter2>
  100. <list :item="items"></list>
  101. </view>
  102. </template>
  103. <script>
  104. import wepy from 'wepy';
  105. //引入List、Panel和Counter组件
  106. import List from '../components/list';
  107. import Panel from '../components/panel';
  108. import Counter from '../components/counter';
  109. export default class Index extends wepy.page {
  110. //页面配置
  111. config = {
  112. "navigationBarTitleText": "test"
  113. };
  114. //声明页面中将要使用到的组件
  115. components = {
  116. panel: Panel,
  117. counter1: Counter,
  118. counter2: Counter,
  119. list: List
  120. };
  121. //可用于页面模板中绑定的数据
  122. data = {
  123. myNum: 50,
  124. syncNum: 100,
  125. items: [1, 2, 3, 4]
  126. }
  127. }
  128. </script>

请点赞!因为你的鼓励是我写作的最大动力!

官方微信公众号

吹逼交流群:711613774

吹逼交流群

发表评论

表情:
评论列表 (有 0 条评论,439人围观)

还没有评论,来说两句吧...

相关阅读

    相关 前端技术前沿10

    允许用户从NPM服务器下载别人编写的第三方包到本地使用。 允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用。 允许用户将自己编写的包或命令行程序上传到NP