Taro启动小程序报错缺少npm包@actions/xxx

报错如图:
在这里插入图片描述
代码如图:

import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { connect } from '@tarojs/redux'
import * as actions from '@actions/cate'
@connect(state => state.cate, { ...actions })
class Cate extends Component {
  config = {
    navigationBarTitleText: '分类'
  }

  componentDidMount() {
  }

  render () {
    return (
      <View className='cate'>
        123
      </View>
    )
  }
}

export default Cate

分析原因:引用路径出错,未配置alias,导致路径报错
在根目录下config文件夹下index.js文件内

  alias: {
    '@actions': path.resolve(__dirname, '..', 'src/actions'),
    '@assets': path.resolve(__dirname, '..', 'src/assets'),
    '@components': path.resolve(__dirname, '..', 'src/components'),
    '@constants': path.resolve(__dirname, '..', 'src/constants'),
    '@reducers': path.resolve(__dirname, '..', 'src/reducers'),
    '@styles': path.resolve(__dirname, '..', 'src/styles'),
    '@utils': path.resolve(__dirname, '..', 'src/utils')
  },

通过上述配置,可以将 src/components 和 src/utils 目录配置成别名,将根目录下的 package.json 和 project.config.json 文件配置成别名,则代码中的引用改写如下:

import * as actions from '@actions/cate'

注意:建议别名使用 @/ 开头而非仅用 @ 开头,因为有小概率会与某些 scoped 形式的 npm 包(行如:@tarojs/taro, @babel/core)产生命名冲突。

参开Taro文档:https://nervjs.github.io/taro/docs/config-detail.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值