根据taro cli创建typescript项目,修改src\pages\index\index.tsx文件:
import Taro, { Component, Config } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import './index.scss'
type State = {
// list: any[]
}
interface Props{}
export default class Index extends Component<Props, State> {
/**
* 指定config的类型声明为: Taro.Config
*
* 由于 typescript 对于 object 类型推导只能推出 Key 的基本类型
* 对于像 navigationBarTextStyle: 'black' 这样的推导出的类型是 string
* 提示和声明 navigationBarTextStyle: 'black' | 'white' 类型冲突, 需要显示声明类型
*/
config: Config = {
navigationBarTitleText: '首页'
}
state = {
list: [
{selected: true,text: 'one'},
{selected: true,text: 'two'},
{selected: true,text: 'three'},
{selected: false,text: 'three'}
]
}
componentWillMount () { }
componentDidMount () { }
componentWillUnmount () { }
componentDidShow () { }
componentDidHide () { }
render () {
const list = this.state.list.map(l => {
if (l.selected) {
return <li>{l.text}</li>
}
}).filter(Boolean)
return (
<View className='index'>
<View className='test'>测试是否自适应web桌面网页</View>
{list}
<Text>Hello world!</Text>
</View>
)
}
}
这样的代码编译也能通过