上一篇css
执行以后 选择选项html
taro init # 依次选择 # ? 请输入项目名称! myDaliyList # ? 请输入项目介绍! 个人日记本 # ? 是否须要使用 TypeScript ? Yes # ? 请选择 CSS 预处理器(Sass/Less/Stylus) Sass # ? 请选择模板 redux
npm install taro-ui
安装完成以后按照官方说明须要给h5配置H5 配置 `esnextModules`react
# 文件 config/index.js 添加 h5: { esnextModules: ['taro-ui'] }
添加完成以后,执行命令:webpack
npm run dev:h5
而后 就报错了 😭git
# 找不到模块 uglifyjs-webpack-plugin UnhandledPromiseRejectionWarning: Error: Cannot find module 'uglifyjs-webpack-plugin'
通常这种问题懒得想的话,就缺什么装什么,因此:github
# 安装uglifyjs-webpack-plugin npm install uglifyjs-webpack-plugin # 安装完成以后再执行 npm run dev:h5 # 如下是输出内容 建立 发现文件 src\app.scss 建立 发现文件 src\app.tsx 建立 发现文件 src\index.html 建立 发现文件 src\store\counter.ts 建立 发现文件 src\pages\index\index.scss 建立 发现文件 src\pages\index\index.tsx ℹ️ Listening at http://0.0.0.0:10086/ 监听文件修改中... ✅ Compiled successfully!
这样就正常执行成功了 访问http://127.0.0.1:10086/能够看到web
# src/app.tsx 下全局添加样式 import 'taro-ui/dist/style/index.scss' // 全局引入一次便可
# src/pages/index/index.tsx引入组件 import { AtButton } from 'taro-ui';
使用组件typescript
render() { const { counterStore: { counter } } = this.props; return ( <View className="index"> <AtButton type="primary" size="small" onClick={this.increment}> + </AtButton> <AtButton type="secondary" size="normal" onClick={this.decrement}> - </AtButton> <AtButton onClick={this.incrementAsync}>Add Async</AtButton> <Text>{counter}</Text> </View> ); }
页面显示出组件效果,则说明引入成功。npm
taro-ui 引入完成以后发现代码有不少地方标红,鼠标移上去能够发现是eslint的错误提示。
Unexpected separator (;).eslint@typescript-eslint/member-delimiter-style
Unexpected usage of doublequote.eslintjsx-quotes
主要是eslint 的默认配置引发的,这里须要稍微修改一下json
修改.eslintrc=>.eslintrc.js
module.exports = { extends: ['taro', 'plugin:@typescript-eslint/recommended'], parser: '@typescript-eslint/parser', rules: { 'no-console': process.env.NODE_ENV === 'production' ? ['warn'] : ['off'], // 生产环境console警告 'no-unused-vars': [ 'error', { varsIgnorePattern: 'Taro' } ], 'react/jsx-filename-extension': [ 1, { extensions: ['.js', '.jsx', '.tsx'] } ], '@typescript-eslint/no-var-requires': ['warn'], '@typescript-eslint/no-unused-vars': [ 'error', { varsIgnorePattern: 'Taro' } ], '@typescript-eslint/member-delimiter-style': [ 'error', { // 结尾加分号 multiline: { delimiter: 'semi', requireLast: true }, singleline: { delimiter: 'semi', requireLast: false } } ], '@typescript-eslint/explicit-function-return-type': 0, '@typescript-eslint/no-empty-function': ['warn'] }, parserOptions: { ecmaFeatures: { jsx: true }, useJSXTextNode: true, project: './tsconfig.json' } };
添加.prettierrc 这里使用的格式化插件是Prettie
{ "jsxSingleQuote": true, "singleQuote": true }
附:react 建议的组件class顺序
class extends React.Component
:static
methodsconstructor
getChildContext
componentWillMount
componentDidMount
componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
componentDidUpdate
componentWillUnmount
onClickSubmit()
oronChangeDescription()
render
_likegetSelectReason()
orgetFooterContent()
renderNavigation()
orrenderProfilePicture()
render