引言 :
学习教程:慕课网-7七月老师的微信小程序入门与实战
此文用来记录一个程序渣学习小程序的踩坑路程。
话不多说,开始记录!
---------------------------------------------------------分割线------------------------------------------------------------------
踩坑001:"?"码成了“?”
onPostTap: function (event) {
var postId = event.currentTarget.dataset.postid;
// console.log("on post id is" + postId);
wx.navigateTo({
url: "post-detail/post-detail?id=" + postId
})
},
当时API wx.navigateTo 里的url元素那一行url: "post-detail/post-detail?id=" + postId 出了问题但问号那里并没有报错找了很久,后来被高人指点才恍然大悟。类似的错误还有很多 逗号、分号、引号、冒号,有时候会花很长时候找这种BUG,还是挺心塞的。
踩坑002:报错:tabBar[0].pagePath "/pages/posts/post" 需在 pages 数组中
{
"pages": [
"pages/posts/post",
"pages/posts/post-detail/post-detail",
"pages/movies/movies",
"pages/welcome/welcome"
],
"window": {
"navigationBarBackgroundColor": "405f80"
},
"tabBar": {
"list": [
{
"pagePath": "pages/posts/post",
"text": "文章"
},
{
"pagePath": "pages/movies/movies",
"text": "电影"
}
]
}
}
这里有两点需要注意:
①app.json里pages的第一项要成为tabBar中的一员
②自己在tabBar里的pagePath路径加多了个“/”
踩坑003:报错:https://api.douban.com 不在以下 request 合法域名列表中,请参考文档:https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html
wx.request({
url: 'https://api.douban.com/v2/movie/top250',
method:'GET',
header:{
"Content-Type":"application/json"
}
})
菜单栏-工具-项目详情-项目设置-勾选不校验合法域名、web-view(业务域名)、TLS 版本以及 HTTPS 证书
踩坑004:报错:GET https://api.douban.com/v2/movie/top250 403 (Forbidden)
onLoad:function(event){
wx.request({
url: 'http://t.yushu.im/v2/movie/top250',
method:'GET',
header:{
"Content-Type":"application/json"
}
})
豆瓣在2018年1月23号的时候进行了域名迁移,将原先的http://api.douban.com域名地址替换为了http://t.yushu.im(对比踩坑003代码)
踩坑004:报错:module "pages/utils/util.js" is not defined
// pages/movies/more-movie/more-movie.js
var app = getApp();
var util = require('../../../utils/util.js')
相对路径问题,要多加“../”
踩坑005:报错:
thirdScriptError
util.convertToCastString is not a function;at pages/movies/movie-detail/movie-detail page processDoubanData function
TypeError: util.convertToCastString is not a function
function convertToCastString(casts) {
var castsjoin = "";
for (var idx in casts) {
castsjoin = castsjoin + casts[idx].name + "/";
}
return castsjoin.substring(0, castsjoin.length - 2);
}
function convertToCastInfos(casts) {
var castsArray = []
for (var idx in casts) {
var cast = {
img: casts[idx].avatars ? casts[idx].avatars.large : "",
name: casts[idx].name
}
castsArray.push(cast);
}
return castsArray;
}
module.exports = {
convertToStarsArray: convertToStarsArray,
http: http,
convertToCastString: convertToCastString,
convertToCastInfos: convertToCastInfos
}
找了挺久,在util.js里定义方法要输出(在module.exports里)
完。