docsify-demo-box-react icon indicating copy to clipboard operation
docsify-demo-box-react copied to clipboard

import and export may only appear at the top level

Open mescalchuan opened this issue 6 years ago • 1 comments

我希望使用react展现自己封装的组件的示例,于是在 md文件中这样写:

<desc>
    ...
</desc>
<style>
    ...
</style>
<script>
    import Counter from './h5/components/counter';
    export default class Application extends React.Component {
         ...
    }
</script>

页面上提示:Uncaught SyntaxError: unknown: 'import' and 'export' may only appear at the top level

原因是import被一个匿名函数包起来了:(function() { import Counter from ... })

请问这个问题怎么解决?难道不能用import吗?谢谢🙏

mescalchuan avatar Mar 08 '19 08:03 mescalchuan

不支持 import 语法,你应该尝试把这个组件挂到全局的变量上(通过作为 umd 组件),然后在你的文档代码里使用。

你可以参考这个文档的做法: https://github.com/sodalife/soui-react/blob/master/docs/index.html

njleonzhang avatar Mar 08 '19 10:03 njleonzhang