首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在SyntaxError中使用轻量级图表时获取NextJS

在SyntaxError中使用轻量级图表时获取NextJS
EN

Stack Overflow用户
提问于 2021-04-12 13:18:54
回答 1查看 1.3K关注 0票数 6

我试图在我的lightweight-charts项目中使用nextjs包,但是当我试图调用createChart函数时,我在nodejs控制台中得到了这个错误。

代码语言:javascript
复制
...\lightweight-charts\dist\lightweight-charts.esm.development.js:7
import { bindToDevicePixelRatio } from 'fancy-canvas/coordinate-space';
^^^^^^

SyntaxError: Cannot use import statement outside a module

构成部分:

代码语言:javascript
复制
import styled from "styled-components"
import { createChart } from 'lightweight-charts';

const Wrapper = styled.div``

const CoinPriceChart = () => {
  const chart = createChart(document.body, { width: 400, height: 300 });
  return <Wrapper></Wrapper>
}

export default CoinPriceChart

页面:

代码语言:javascript
复制
import styled from "styled-components"
import CoinPriceChart from "../../components/charts/CoinPriceChart"

const Wrapper = styled.div``

const CoinDetailPage = () => {
  return (
    <Wrapper>
      <CoinPriceChart />
    </Wrapper>
  )
}

export default CoinDetailPage

,有人知道我能做些什么吗?,谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-12 14:15:28

这是因为您试图在SSR上下文中导入库。使用next.js Dynamicssr : false应该可以解决以下问题:

代码语言:javascript
复制
import styled from "styled-components"
import dynamic from "next/dynamic";
const CoinPriceChart = dynamic(() => import("../../components/charts/CoinPriceChart"), {
  ssr: false
});

const Wrapper = styled.div``

const CoinDetailPage = () => {
  return (
    <Wrapper>
      <CoinPriceChart />
    </Wrapper>
  )
}

export default CoinDetailPage
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67059133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档