// pages/blog/blog.js //获取应用实例 const app = getApp(); Component({ /** * 继承父级样式 */ options: { addGlobalClass: true, }, /** *组件的初始数据 */ data: { content:'子组件的content', homeHeaderItem:'', isLoading: true, article: {'key':12}, baseUrl:'https://yongma16.xyz/api/', path:'article/index/', articleTitle:[], currentTitle:'', }, observers:{ }, lifetimes: { // 生命周期函数,可以为函数,或一个在 methods 段中定义的方法名 attached: function () { }, moved: function () { }, detached: function () { }, }, // 生命周期函数,可以为函数,或一个在 methods 段中定义的方法名 // attached: function () { }, // 此处 attached 的声明会被 lifetimes 字段中的声明覆盖 // ready: function() { }, lifetimes: { // 生命周期函数,可以为函数,或一个在 methods 段中定义的方法名 attached: function () { this.getRemoteArticle() }, moved: function () { }, detached: function () { }, }, methods: { getRemoteArticle:function(){ this.setData({ isLoading:true }) const baseUrl=this.data.baseUrl const path=this.data.path const headers = {'Content-Type':'application/json;charset=UTF-8'} const that=this wx.request({ url: baseUrl+path, headers:headers, method:'GET', success: (res) => { const data=res.data console.log(data,'data') const title=data&&data.title const articleObjectId=data&&data.articleObjectId const articleTitle=title.map((item,index)=>{ return { title:item, id:index } }) const articleStr=data&&data.article&&data.article[0] const markdownText = app.changeMrkdownText(articleStr) that.setData({ isLoading:false, currentTitle:articleTitle&&articleTitle[0].title, article:markdownText, articleTitle:articleTitle }) // 触发父组件事件 that.triggerEvent('updateArticleMenu', articleTitle) } }); }, /** * 配置加载 */ setLoading(status){ this.setData({ isLoading:status, article:undefined }) }, /** * 父组件调用更新文章 * @param {*} articleStr * @param {*} title */ setArticleNode(articleStr,title){ const markdownText = app.changeMrkdownText(articleStr); this.setData({ isLoading:false, article:markdownText, currentTitle:title }) }, clearArticleNode(){ this.setData({ article:null, currentTitle:'加载中...' }) }, onMyButtonTap: function(){ this.setData({ // 更新属性和数据的方法与更新页面数据的方法类似 }) }, // 内部方法建议以下划线开头 _myPrivateMethod: function(){ // 这里将 data.A[0].B 设为 'myPrivateData' // this.setData({ // 'A[0].B': 'myPrivateData' // }) }, _propertyChange: function(newVal, oldVal) { } } })