页面跳转传值

小程序中没有a链接这一标签,取而代之的是页面链接 navigator

url 代表当前小程序内的跳转链接

页面跳转

<navigator url="../shownews/shownews?id={{item.id}}" hover-class="navigator-hover"></navigator>

在app.json中添加一个"pages/shownews/shownews",页面,保存后微信开发者工具,会自动为我们创建一个新页面。

根据新页面的位置修改 navigator 中url的路径,使其能够跳转到 shownews 页面

页面传值

1、在详情页面 index.js 中引用 api.js 文件

const api = require('../../utils/api.js');  //路径根据自己的实际情况调整

2、在详情页面 index.js 中编辑生命周期函数--监听页面加载

var that = this;

// 获取 navigator 传过来的产品详情id

that.id = options.id;

that.getContent();

3、将获取到的that.id参数传递到api获取数据函数中

getContent() {

  let that = this;

  api.getContent(that.id).then(function (news) {

    // 初始化新闻详情

    that.setData({

    getContent: news.data,

    })

  })

},

4、在页面中调用获取到的数据

<view>{{getContent.content}}</view>

问题

可以看到此时点击首页跳转到详情页面时,获取到的是带有html标签的文本,并非后台编辑器填写的内容,这是因为微信默认是不对富文本进行解析的。

 阅 528