赞
踩
npm install -g @tarojs/cli
yarn global add @tarojs/cli
taro init taro-app
安装 taro-ui
cd taro-app
yarn add taro-ui
全局引入taro-ui样式
import 'taro-ui/dist/style/index.scss'
page
对应项目路径lazyCodeLoading
组件按需注入Taro.navigateTo({url: xxx})
小程序内部跳转export default {
"lazyCodeLoading": "requiredComponents",
pages: [
'pages/index/index',
'pages/statistics/index',
'pages/my/index',
'pages/about/index',
'pages/contact/index',
'pages/webview/index'
],
// ...
}
src/app.ts
中添加Taro.showShareMenu
class App extends Component {
render() {
Taro.showShareMenu({
withShareTicket: true,
// @ts-ignore
menus: ['shareAppMessage', 'shareTimeline'],
showShareItems: ['shareAppMessage', 'shareTimeline'],
});
// this.props.children 是将要会渲染的页面
return this.props.children
}
}
OfficialAccount
import { OfficialAccount } from '@tarojs/components'
<OfficialAccount />
视图
Taro.getUserProfile
获取用户的头像昵称Taro.setStorageSync
存储信息 相当于 localStorage.setItem
Taro.getStorageSync
获取信息 相当于 localStorage.getItem
getUserProfile() {
Taro.getUserProfile({
desc: '获取用户昵称、头像',
success: (res) => {
Taro.setStorageSync('userInfo', res.userInfo)
this.setState({ userInfo: res.userInfo })
},
fail: () => {
console.error("您拒绝了请求");
return;
}
})
}
打印 getUserProfile success(res)
getCurrentInstance
获取地址栏/routerimport { FC, useEffect, useState } from 'react'
import { View, WebView } from '@tarojs/components'
import { getCurrentInstance } from '@tarojs/taro'
const MyWebView: FC = () => {
const [hrefURL, setHrefURL] = useState<string>('')
useEffect(() => {
const { params } = getCurrentInstance().router ?? {}
setHrefURL(decodeURIComponent(params?.webUrl ?? ''))
}, [])
return (
<View className='webview' >
<WebView src={hrefURL} />
</View>
)
}
export default MyWebView
打印 getCurrentInstance()
跳转的公众号需要是企业认证的,才能配置业务域名,个人公众号暂不支持
WebView
handleGridClick(item: itemDTO) {
Taro.navigateTo({ url: `/pages/webview/index?webUrl=${encodeURIComponent(item.url)}` })
}
跳转
Taro.getSystemInfoSync()
获取设备屏幕高度scrollY
、height
const { windowHeight } = Taro.getSystemInfoSync()
const scrollHeight = (windowHeight * windowHeight / 750 - 140)
const scrollStyle = {
height: scrollHeight + 'px',
}
<ScrollView
scrollY
enhanced={true}
show-scrollbar={false}
scrollWithAnimation={true}
enable-back-to-top={true}
style={scrollStyle}
>
// ...
</ScrollView>
打印 getSystemInfoSync()
echarts-for-weixin
ec-canvas
拷贝到自己项目中src下canvas
设置宽高需要在
index.config.ts
声明ec-canvas
组件
export default {
navigationBarTitleText: '前端进阶技术栈',
usingComponents: {
"ec-canvas": "../../ec-canvas/ec-canvas"
}
}
index.ts 使用
function getPieChart(canvas, width, height, dpr) {
const chart = echarts.init(canvas, null, {
width: width,
height: height,
devicePixelRatio: dpr // 像素
});
canvas.setChart(chart);
var option = {
tooltip: {
show: true
},
legend: {
top: 'bottom'
},
series: [
{
name: 'Nightingale Chart',
type: 'pie',
radius: [35, 110],
center: ['50%', '50%'],
roseType: 'area',
itemStyle: {
borderRadius: 8
},
data: [
{ value: 56.7, name: 'Vue' },
{ value: 36.4, name: 'Angular' },
{ value: 72.5, name: 'React' }
]
}
]
}
chart.setOption(option);
return chart;
}
// ...
this.state = {
ecPie: {
onInit: getPieChart
}
}
// ...
<View className="canvas pie">
<ec-canvas id="mychart-dom-pie" canvas-id="mychart-pie" ec={ecPie}></ec-canvas>
</View>
在 global.d.ts 中添加以下内容
declare namespace JSX {
interface IntrinsicElements {
'ec-canvas': any,
...
}
}
视图
EChart 相关文档
wemark
wemark
文件拷贝到自己的项目的src目录下设置编译时复制wemark目录,修改
config/index.js
,在copy设置项中增加wemark,
copy: {
patterns: [
{
from: 'src/wemark',
to: 'dist/wemark',
},
],
options: {
}
},
设置taro编译时忽略
remarkable.js
,继续修改config/index.js
weapp: {
compile: {
exclude: [
'src/wemark/remarkable.js',
]
},
...
}
在 global.d.ts 中添加以下内容
declare namespace JSX {
interface IntrinsicElements {
'wemark': any
}
}
import { FC, useEffect, useState } from 'react'
import { View } from '@tarojs/components'
import { getCurrentInstance } from '@tarojs/taro'
import './index.scss'
import Taro from '@tarojs/taro'
const Article: FC = () => {
const [markdown, setMarkdown] = useState<string>('')
useEffect(() => {
const { md, title } = getCurrentInstance().router?.params ?? {}
setMarkdown(decodeURIComponent(md ?? ''))
Taro.setNavigationBarTitle({
title: decodeURIComponent(title ?? '前端进阶技术栈')
})
}, [])
return (
<View className='article'>
<wemark md={markdown} link={true} highlight={true} type='wemark' />
</View>
)
}
export default Article
web端调试
yarn dev:h5
web端调试微信内置 API 需要小程序开发工具调试
yarn build:weapp
dist
文件夹即可Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。