Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

你好,出现一个问题 invalid componentPath: #9

Closed
kami1983 opened this issue Mar 19, 2019 · 9 comments
Closed

你好,出现一个问题 invalid componentPath: #9

kami1983 opened this issue Mar 19, 2019 · 9 comments

Comments

@kami1983
Copy link

无论我如何修改路径都是报错:invalid componentPath:

我的代码:(路径绝对是正确的,请问是什么原因造成的呢)

const id = simulate.load(path.join(__dirname, '../components/index')) // 加载自定义组件,返回组件 id
// const id = simulate.load('/components/index')

@rubbishquan
Copy link

用process.cwd()替代__dirname试试

@JuneAndGreen
Copy link
Contributor

有两种可能性:
1、可能环境判断不正确,重新安装一下 miniprogram-simulate 试试?保证这个包和相关依赖都是最新版本
2、路径不是绝对路径,可以输出一下 path.join(__dirname, '../components/index') 看看?

@JerryYuanJ
Copy link

JerryYuanJ commented Mar 21, 2019

@kami1983 你可以将

const id = simulate.load(path.join(__dirname, '../components/index'))
改成

const id = simulate.load(path.join(__dirname, '../components/index/index')) 试试

参考源码:

/**
 * 注册自定义组件
 */
function register(componentPath, tagName, cache) {
  if (typeof componentPath === 'object') {
    // 直接传入定义对象
    const definition = componentPath

    return jComponent.register(definition)
  }

  if (typeof componentPath !== 'string') {
    throw new Error('componentPath must be a string')
  }

  if (!tagName || typeof tagName !== 'string') {
    tagName = 'main' // 默认标签名
  }

  const oldLoad = nowLoad
  const component = nowLoad = {
    tagName,
    json: _.readJson(`${componentPath}.json`),
  }

  if (!component.json) {
    throw new Error(`invalid componentPath: ${componentPath}`)
  }

实际上是要能获取组件对应的json配置文件的。

@kami1983
Copy link
Author

已经解决,谢谢大家!

@smilebuz
Copy link

已经解决,谢谢大家!

您好 我想请问下是如何解决的 因为我也遇到了这个问题 谢谢

@katoto
Copy link

katoto commented Dec 9, 2019

问题应该出在“usingComponents” ,里头如果路径有问题就会造成

@arleyGuoLei
Copy link

怎么解决的?

@fangxxiaoming
Copy link

小程序组件里usingComponents引用的是绝对路径的,运行时候也提示路径问题,invalid componentPath,现在是无法支持吗

@yrmatou
Copy link

yrmatou commented Mar 24, 2022

配置 jest moduleNameMapper属性就可以了 https://jestjs.io/zh-Hans/docs/configuration#modulenamemapper-objectstring-string--arraystring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants