Taro使用小结

本文介绍了一种在小程序中配置路由的方法,通过在app.js中指定页面路径即可实现导航。此外,还详细说明了如何在代码中引用图片资源,包括直接在HTML中使用以及在循环数组中动态加载图片的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.不需要注册路由,只需要在app.js里写上文件对应的路径即是路由:

config = {
    pages: [
      'pages/case/index',
      'pages/index/index',
    ],
    window: {
      backgroundTextStyle: 'light',
      navigationBarBackgroundColor: '#fff',
      navigationBarTitleText: 'WeChat',
      navigationBarTextStyle: 'black'
    }
  }

访问路径:http://127.0.0.1:10086/#/pages/solution/index

2.图片引用使用

<Image className='content-catalog-icon' src={require('../../assets/images/solution/icon_P01.png')}></Image>

但如果是使用数组循环时,需要在数组中使用require,而不是html代码中

 

//数据
contentList:[
        {
            id : "safe",
            name : "云安全解决方案",
            icon : require("../images/solution/icon_P01.png"),
            articleList : [{
                articleId:1,
                title:"111111",
                url: 'https://mp.weixin.qq.com/mp/homepage?__biz=MjM5MjcyMjU5NA%3D%3D&hid=11&sn=0a578331a3065e6bf0ad2262c7db6a27&scene=18'
            }]

        },
       
    ]


//js
import solutionList from '../../assets/json/solutions'

render () {
   // 列表
    const contentList = (
      solutionList.contentList.map((item,index) => {
        let content = (<View className="content" id={item.id}>
        <View className="content-catalog">
          <Image className='content-catalog-icon' src={item.icon}></Image>
          <View className="content-catalog-txt">{item.name}</View>
        </View>
        </View>)

        return content
      })
    )


<View>{contentList} </View>

}









 

Taro使用for循环可以通过使用JSX语法来实现。你可以使用数组的map方法来遍历一个数组并生成对应的JSX元素。例如,假设你有一个数组`data`,你可以通过以下方式在Taro使用for循环: ``` {data.map((item, index) => ( // 在这里添加你要生成的JSX元素 ))} ``` 其中,`data.map`会遍历数组`data`中的每个元素,并返回一个新的数组,新数组中的每个元素都是按照你提供的回调函数的返回值生成的。在回调函数中,`item`表示当前遍历的元素,`index`表示当前元素在数组中的索引。 你可以在这个回调函数中生成需要的JSX元素,然后将它们放在外层的大括号中,这样就可以在Taro使用for循环了。例如,如果你想生成一组`View`组件,可以这样写: ``` {data.map((item, index) => ( <View key={index}> {item} </View> ))} ``` 这样就可以根据数组`data`中的元素生成对应的`View`组件了。请注意,为了确保每个生成的元素都有唯一的`key`属性,你可以使用`index`作为`key`值。 总结起来,在Taro使用for循环遍历一个数组可以通过使用数组的map方法,结合JSX语法来实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [taro 使用echarts图表.zip](https://download.youkuaiyun.com/download/weixin_40532650/12737545)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [小程序第三方框架对比 ( wepy / mpvue / taro )](https://blog.youkuaiyun.com/weixin_42333548/article/details/103487410)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值