成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

微信小程序教學(xué)第二章(含視頻):小程序中級(jí)實(shí)戰(zhàn)教程之預(yù)備篇 - 提取util公用方法 |基于最新版1

hlcfan / 1251人閱讀

摘要:官網(wǎng)訪問(wèn)官網(wǎng)更快閱讀全部免費(fèi)分享課程出品全網(wǎng)最新微信小程序基于最新版開(kāi)發(fā)者工具之初中級(jí)培訓(xùn)教程分享。翻譯能貓聲明本雙語(yǔ)文章的中文翻譯系英語(yǔ)原創(chuàng)內(nèi)容,轉(zhuǎn)載請(qǐng)注明出處。

iKcamp官網(wǎng):http://www.ikcamp.com

訪問(wèn)官網(wǎng)更快閱讀全部免費(fèi)分享課程:《iKcamp出品|全網(wǎng)最新|微信小程序|基于最新版1.0開(kāi)發(fā)者工具之初中級(jí)培訓(xùn)教程分享》。
包含:文章、視頻、源代碼

第二章:小程序中級(jí)實(shí)戰(zhàn)教程之預(yù)備篇

本文配套視頻地址
https://v.qq.com/x/page/m0554uf5jzt.html?new=1

util公共方法

開(kāi)始前請(qǐng)把 ch2-2 分支中的 code/ 目錄導(dǎo)入微信開(kāi)發(fā)工具

在這一章中,我們會(huì)在util目錄新建 3 個(gè)文件,這些文件分別提供了一些常用的函數(shù),變量,已便我們?cè)诓煌捻?yè)面中使用

  ├── utils/             
  │   ├─ config.js
  │   ├─ index.js
  │   ├─ mock.js
Step 1. ? config.js 配置常量

該文件存放了一些 const 常量(如開(kāi)發(fā)環(huán)境,默認(rèn)文案,默認(rèn)圖片等),將這些常量存在 core 這個(gè)對(duì)象中,最后通過(guò) export default core 將 core 這個(gè)對(duì)象導(dǎo)出。由于只是存放一些配置常量,此處不再?gòu)?fù)述:

  "use strict";
  const env = "dev";// dev production
  /*
   * 默認(rèn)接口出錯(cuò)彈窗文案
   * @type {string}
   */
  const defaultAlertMessage = "好像哪里出了小問(wèn)題~ 請(qǐng)?jiān)僭囈淮蝵";
  /*
   * 默認(rèn)分享文案
   * @type {{en: string}}
   */
  const defaultShareText = {
      en: "iKcamp英語(yǔ)-學(xué)英語(yǔ)口語(yǔ)聽(tīng)力四六級(jí)"
  };
  /*
   * 小程序默認(rèn)標(biāo)題欄文字
   * @type {string}
   */
  const defaultBarTitle = {
      en: "iKcamp英語(yǔ)學(xué)習(xí)"
  };
  /*
   * 文章默認(rèn)圖片
   * @type {string}
   */
  const defaultImg = {
      articleImg: "https://n1image.hjfile.cn/mh/2017/06/07/7e8f7b63dba6fa3849b628c0ce2c2a46.png",
      coverImg: "https://n1image.hjfile.cn/mh/2017/06/07/7472c035ad7fe4b8db5d2b20e84f9374.png"
  };
  let core = {
      env: env,
      defaultBarTitle: defaultBarTitle["en"],
      defaultImg: defaultImg,
      defaultAlertMsg: defaultAlertMessage,
      defaultShareText: defaultShareText["en"],
      isDev: env === "dev",
      isProduction: env === "production"
  };
  export default core;
Step 2. ? mock.js 數(shù)據(jù)

該文件用來(lái)存放一些假數(shù)據(jù),方便本地開(kāi)發(fā)調(diào)試,如果你有 mock 服務(wù)器,當(dāng)然更好

  const list = {
    data: [
      {
        articles: [
          {
            contentId: 1177432,
            cover: "https://n1image.hjfile.cn/mh/2017/06/26/9ffa8c56cfd76cf5159011f4017f022e.jpg",
            pubdate: "2017-06-27 00:45:00",
            title: "Quora精選:為什么聰明人總能保持冷靜?"
          },
          {
            contentId: 1176561,
            cover: "https://n1image.hjfile.cn/mh/2017/06/26/0eb29c4c08e7be93f74ce9ce0fd5a25e.jpg",
            pubdate: "2017-06-27 00:30:00",
            title: "與時(shí)間有關(guān)的英語(yǔ)俚語(yǔ)(四)"
          },
          {
            contentId: 1161967,
            cover: "http://n1image.hjfile.cn/mh/2017/05/25/5d6ce79848cc81b800959383c35dd081.jpg",
            pubdate: "2017-06-27 00:15:00",
            title: "2017.5大學(xué)英語(yǔ)六級(jí)口語(yǔ)考試真題:看圖論述“人與人之間的信任”"
          },
          {
            contentId: 1177828,
            cover: "https://n1image.hjfile.cn/mh/2017/06/26/c99a6d180367bf1a250d6e7c6c3083c6.jpg",
            pubdate: "2017-06-27 00:00:01",
            title: "亞馬遜賣(mài)衣服有新招!新衣服試穿一周再付錢(qián)"
          }
        ],
        date: "2017-06-27"
      },
      {
        articles: [
          {
            contentId: 1175100,
            cover: "https://n1image.hjfile.cn/mh/2017/06/23/2490fdbcfc131c0e256546da4a03f1ac.jpg",
            pubdate: "2017-06-26 00:45:00",
            title: "雙語(yǔ)美文:小時(shí)候的自己,真是傻得可愛(ài)"
          },
          {
            contentId: 1172434,
            cover: "https://n1image.hjfile.cn/mh/2017/06/19/57a8ef65ee53f5921657376fd3fe4a75.jpg",
            pubdate: "2017-06-26 00:30:00",
            title: "15個(gè)和money有關(guān)的習(xí)語(yǔ),可別用錯(cuò)了"
          },
          {
            contentId: 1161968,
            cover: "http://n1image.hjfile.cn/mh/2017/05/25/2260fb0deb74734f5ec5a375423a6b52.jpg",
            pubdate: "2017-06-26 00:15:00",
            title: "2017年5月大學(xué)英語(yǔ)六級(jí)口語(yǔ)考試真題——小組討論之扶老人是我們的責(zé)任嗎?"
          },
          {
            contentId: 1161963,
            cover: "http://n1image.hjfile.cn/mh/2017/05/25/93228c5713545ef328e708ddb73e28a2.jpg",
            pubdate: "2017-06-26 00:15:00",
            title: "2017年5月大學(xué)英語(yǔ)六級(jí)口語(yǔ)考試——優(yōu)秀領(lǐng)導(dǎo)的特質(zhì)"
          }
        ],
        date: "2017-06-26"
      },
      {
        articles: [
          {
            contentId: 1175110,
            cover: "https://n1image.hjfile.cn/mh/2017/06/23/a5bf85e439696829a937accfd99f0eb7.jpg",
            pubdate: "2017-06-25 00:45:00",
            title: "Quora精選:什么樣的人注定無(wú)法成功?"
          },
          {
            contentId: 1142849,
            cover: "https://n1image.hjfile.cn/mh/2017/06/22/faf544ccf3e4f9e8da4ee9d5d8fcc635.jpg",
            pubdate: "2017-06-25 00:30:00",
            title: "表達(dá)雌性動(dòng)物和雄性動(dòng)物的不同說(shuō)法"
          },
          {
            contentId: 1161915,
            cover: "http://n1image.hjfile.cn/mh/2017/05/25/7778bd5547fa216db57cd87a78bd790d.jpg",
            pubdate: "2017-06-25 00:15:00",
            title: "2017年5月大學(xué)英語(yǔ)四級(jí)口語(yǔ)考試——如何用PPT展現(xiàn)一堂課"
          },
          {
            contentId: 1175101,
            cover: "https://n1image.hjfile.cn/mh/2017/06/23/045ed76502b0f1ccdaccdae827c17c2c.jpg",
            pubdate: "2017-06-25 00:00:00",
            title: "雙語(yǔ)美文:總在不經(jīng)意間,發(fā)覺(jué)自己渺小"
          }
        ],
        date: "2017-06-25"
      }
    ],
    message: "success",
    status: 0,
    time: null
  };

  const detail = {
    data: {
      agreeNum: 0,
      articleImage: "https://n1image.hjfile.cn/mh/2017/06/26/9ffa8c56cfd76cf5159011f4017f022e.jpg",
      author: "Quora用戶(hù)",
      autoSummary: "Why do intelligent people seem calm all the time?為什么聰明人總能保持冷靜?

	

	Answer from @Domen Grabec來(lái)自@Domen Grabec的回答

	If you can solve your problem, then what is the n...",
      cateId: 0,
      channelId: 1,
      commentCount: 2,
      content: "

Why do intelligent people seem calm all the time?
為什么聰明人總能保持冷靜?

 

Answer from @Domen Grabec
來(lái)自@Domen Grabec的回答

If you can solve your problem, then what is the need of worrying?
如果你能解決問(wèn)題,那何慮之有?

If you cannot solve it, then what is the use of worrying?
如果你不能解決問(wèn)題,那憂(yōu)慮又有什么用呢?

And if you look at this logically not being calm will only work to your disadvantage in many situations:
如果你從邏輯的角度來(lái)看,不冷靜只會(huì)起反效果,在很多情況下都一樣:

There is an important exam coming up and you are freaking out. Panicking might confuse you, inhibit your learning and as a result you might perform worse on the exam.
(比如)你正面臨一場(chǎng)重要的考試,而你慌得不得了。你的驚慌只會(huì)讓你思維混亂,它會(huì)妨礙你的學(xué)習(xí),其結(jié)果就是你會(huì)在考試中發(fā)揮得更糟。

Someone is robbing you and you start to panic. You might get the robber nervous, and he might do something impulsive (like stab you) obviously worsening your position.
(再比如)一個(gè)人正在搶劫你,而你開(kāi)始慌了。你可能會(huì)讓搶劫者變得緊張,而他可能就會(huì)做出些過(guò)激的事情(比如捅你一刀),顯然這回讓你的處境更糟。

You lead a group of people at work and are facing a heavy problem, or an impossible deadline. Well this is the time to inspire people and get the most out of them as possible. That should increase your chances of solving the problem. If you panic you might discourage them, and not only you, but your whole team shall underperform.
(再比如)你正帶領(lǐng)著一組人一起工作,并且正面臨著一個(gè)嚴(yán)峻的問(wèn)題,或者是一個(gè)不可能趕上的截止日期。好嘛,你現(xiàn)在需要做的是啟發(fā)這些人,盡可能讓他們做出成果;這會(huì)讓你解決問(wèn)題的概率更大一些。如果你慌了,你可能會(huì)打擊到他們,于是不只是你,你的整個(gè)團(tuán)隊(duì)都會(huì)發(fā)揮不佳。

While you panic, your mind is having a hard time finding a solution to the problem. Even if the solution is only to stay calm and wait for the problem to pass.
當(dāng)你慌張的時(shí)候,你的大腦會(huì)很難找到解決問(wèn)題的方法。而解決問(wèn)題的方法很有可能只是冷靜下來(lái)并等著問(wèn)題自己過(guò)去。

 

Answer from @Faith Paul
來(lái)自@Faith Paul的回答

Intelligent people think before they speak.
聰明的人先想再說(shuō)。

Intelligent people also think rationally.
而且,聰明人的思維是理性的。

Instead of freaking out over every little thing, finding drama, or becoming a victim, intelligent people rationalize their problems.
聰明人會(huì)理性地去分析問(wèn)題,而不是在每個(gè)細(xì)枝末節(jié)上的慌神、制造情況、或怨天尤人。

Intelligent people don't prolong their problems. They end their suffering by doing what needs to be done in order for that problem to go away.
聰明人不會(huì)讓問(wèn)題遺留下來(lái)。他們會(huì)做那些必要的事以解決問(wèn)題。

Additionally, I've noticed that intelligent people who are stressed find an outlet or take a nap. Bottling up anxiety or pointing fingers like many people do is just going to evoke more stress.
還有,我發(fā)現(xiàn)聰明人在不堪重負(fù)的時(shí)候會(huì)找個(gè)發(fā)泄方式,或者小憩一下。很多其他人或把焦慮壓制下來(lái),或者在別人身上出氣,而這只會(huì)讓壓力更重。

Obviously not all problems can be solved, but my point is that intelligent people handle stress differently.
顯然,并不是所有的問(wèn)題都能被解決,我想表達(dá)的是:聰明人處理壓力的方式時(shí)不一樣的。


Answer from @Humberto Rada
來(lái)自@Humberto Rada的回答

This could be for several reasons, but one thing I noticed from highly intelligent friends who are calm is that they are able to entertain themselves.
原因有很多,而我從我那些冷靜的聰明朋友身上發(fā)現(xiàn)的一點(diǎn)是:他們有辦法自?shī)首詷?lè)。

Yes that is right, they always got something to think about.
對(duì),就是這樣,他們總有什么東西可以想。

Its like they enjoy their thoughts more than interacting with other people.
好像他們覺(jué)得自己的思維比和人交流更有吸引力。

They are always elaborating on thoughts so they are busy with themselves.
他們總是在細(xì)細(xì)揣摩自己的想法,所以他們自己一個(gè)人忙著呢。

 

(翻譯:能貓)

聲明:本雙語(yǔ)文章的中文翻譯系iKcamp英語(yǔ)原創(chuàng)內(nèi)容,轉(zhuǎn)載請(qǐng)注明出處。中文翻譯僅代表譯者個(gè)人觀點(diǎn),僅供參考。如有不妥之處,歡迎指正。

", contentId: 1177432, editorName: "能貓", isBest: "Y", langs: "en ", langsCate: 1043, langsParentCate: 1040, lastUpdateTime: "2017-06-27 00:45:00.530", link: "", maxPageNum: 1, newTitle: "", picName: "", reviewFlag: 0, sourceLink: "", sourceName: "Quora", sourceType: 0, sparePic: "", subTitle: "", subjectId: 0, summary: "為什么有些人總是有辦法冷靜地面對(duì)一切?", tags: "智力|職場(chǎng)|學(xué)習(xí)|雙語(yǔ)閱讀|勵(lì)志人生|實(shí)用熱門(mén)|quora|微信熱門(mén)", title: "Quora精選:為什么聰明人總能保持冷靜?", totalView: 112 }, message: "success", status: 0, time: null }; export { detail, list }
Step 3. ? 增加 index.js 主入口文件

index.js 文件中,我們會(huì)增加一些公用的函數(shù)方法,比如對(duì)網(wǎng)絡(luò)請(qǐng)求封裝、開(kāi)發(fā)環(huán)境下 log 日志、本地存儲(chǔ)等

首先,我們把配置信息 config.js 和假數(shù)據(jù) mock.js 引進(jìn)來(lái),并正常導(dǎo)出

  "use strict"
  import Promise from "../lib/promise"
  import config from "./config"
  import * as Mock from "./mock"

  let util = {
    
  }
  export default util

第二行代碼:

  import Promise from "../lib/promise"

這一段代碼的作用是引入一個(gè)實(shí)現(xiàn) Promise 的 第三方工具庫(kù),這樣我們就可以在小程序中使用 Promise 方法來(lái)解決 回調(diào)地獄 的問(wèn)題。

注意: 在新版本的小程序中已經(jīng)支持了原生 Promise 方法

下面我們開(kāi)始對(duì) util 對(duì)象進(jìn)行公用方法擴(kuò)展封裝:
增加 log 控制臺(tái)調(diào)試功能

如果環(huán)境配置為dev環(huán)境,則打印參數(shù)內(nèi)容

let util = {
  isDEV: config.isDev,
  log(){
    this.isDEV && console.log(...arguments)
  }
}

封裝 alert 彈出窗口

由于開(kāi)發(fā)環(huán)境中,經(jīng)常需要查看一些對(duì)象信息,所以增加了 object 類(lèi)型解析

let util = {
  // 此處省略部分代碼
  alert(title = "提示", content = config.defaultAlertMsg) {
    if ("object" === typeof content){
      content = this.isDEV && JSON.stringify(content) || config.defaultAlertMsg
    }
    wx.showModal({
      title: title,
      content: content
    })
  }
}

wx.showModal 是微信官方提供的 api 方法,具體用法可 查閱文檔

封裝本地緩存數(shù)據(jù)的讀取功能

wx.getStorage 文檔地址
wx.setStorage 文檔地址

let util = {
  // 此處省略部分代碼
  getStorageData(key, cb) {
    let self = this;

    // 將數(shù)據(jù)存儲(chǔ)在本地緩存中指定的 key 中,會(huì)覆蓋掉原來(lái)該 key 對(duì)應(yīng)的內(nèi)容,這是一個(gè)異步接口
    wx.getStorage({
      key: key,
      success (res) {
        cb && cb(res.data);
      }, 
      fail (err) {
        let msg = err.errMsg || "";
        if( /getStorage:fail/.test(msg) ) {
          self.setStorageData(key)
        }
      }
    })
  },
  setStorageData(key, value = "", cb) {
    wx.setStorage({
      key: key,
      data: value,
      success() {
        cb && cb();
      }
    })
  }
}

上一篇-微信小程序教學(xué)第二章:小程序中級(jí)實(shí)戰(zhàn)教程之預(yù)備篇 - 項(xiàng)目結(jié)構(gòu)設(shè)計(jì) |基于最新版1.0開(kāi)發(fā)者工具

下一篇中,我們會(huì)把網(wǎng)絡(luò)請(qǐng)求封裝起來(lái),并增加本地 mock 功能

iKcamp原創(chuàng)新書(shū)《移動(dòng)Web前端高效開(kāi)發(fā)實(shí)戰(zhàn)》已在亞馬遜、京東、當(dāng)當(dāng)開(kāi)售。

相關(guān)文章:
聽(tīng)說(shuō) 2017 你想寫(xiě)前端?
前端開(kāi)發(fā)者指南(2017)
翻譯連載 |《你不知道的JS》姊妹篇 | JavaScript 輕量級(jí)函數(shù)式編程
翻譯 | 關(guān)鍵CSS和Webpack: 減少阻塞渲染的CSS的自動(dòng)化解決方案

iKcamp最新活動(dòng)

報(bào)名地址:http://www.huodongxing.com/ev...

“天天練口語(yǔ)”小程序總榜排名第四、教育類(lèi)排名第一的研發(fā)團(tuán)隊(duì),面對(duì)面溝通交流。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/89154.html

相關(guān)文章

發(fā)表評(píng)論

0條評(píng)論

hlcfan

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<