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

nextjs v13 没有样式 #6172

Closed
yejiang1015 opened this issue May 22, 2023 · 13 comments
Closed

nextjs v13 没有样式 #6172

yejiang1015 opened this issue May 22, 2023 · 13 comments
Labels

Comments

@yejiang1015
Copy link

Version of antd-mobile

5.30.0

Operating system and its version

iOS, Android

Browser and its version

No response

Sandbox to reproduce

No response

What happened?

nextjs v13.4.3
antd-mobile 5.30.0

没有样式

Relevant log output

[antd-mobile: Global] The px tester is not rendering properly. Please make sure you have imported `antd-mobile/es/global`.
@yejiang1015
Copy link
Author

@cwjjjjj
Copy link

cwjjjjj commented May 22, 2023

我也遇见了

@bigbigbo
Copy link

+1

@afc163
Copy link
Member

afc163 commented May 23, 2023

[antd-mobile: Global] The px tester is not rendering properly. Please make sure you have imported antd-mobile/es/global.

按提示加上这个,看上去就好了:https://codesandbox.io/p/sandbox/youthful-meadow-zlc3qb?file=%2Fapp%2Fpage.tsx%3A3%2C38

图片

@cwjjjjj
Copy link

cwjjjjj commented May 24, 2023

[antd-mobile: Global] The px tester is not rendering properly. Please make sure you have imported antd-mobile/es/global.

按提示加上这个,看上去就好了:codesandbox.io/p/sandbox/youthful-meadow-zlc3qb?file=%2Fapp%2Fpage.tsx%3A3%2C38

图片

加上了这个 虽然不显示报错了 但是依然没有样式

@afc163
Copy link
Member

afc163 commented May 24, 2023

我这个 codesandbox 里看着是好的,是么?

@miracles1919
Copy link
Contributor

image

这个蓝色的就是样式呀

@cwjjjjj
Copy link

cwjjjjj commented May 24, 2023

但是我每次使用 antd 组件的时候都需要引入一下这个文件吗

@miracles1919
Copy link
Contributor

我自己试了下是不用的,参考 https://github.com/miracles1919/antd-mobile-examples/tree/master/next-13

可能是 codesandbox 的问题

@bigbigbo
Copy link

bigbigbo commented May 25, 2023

我自己试了下是不用的,参考 https://github.com/miracles1919/antd-mobile-examples/tree/master/next-13

可能是 codesandbox 的问题

我看demo中,next.config.js已经不需要配置transpilePackages: ['antd-mobile'],了吗?
奇怪,demo确实没法复现...但实际项目中又有

@dislido
Copy link
Contributor

dislido commented Jun 2, 2023

看起来是因为import {xxx} from 'antd-mobile'在模块解析时变成直接引入了antd-mobile/es/components/xxx, 没有通过antd-mobile/es/index,而antd-mobile/es/global是在antd-mobile/es/index中引入的
应该只有在以esm方式使用时会出现此问题,上面demo中的cjs方式不会有此问题

@Ruler1020730
Copy link

实测 开发环境 使用 --turbo 会有样式 也不用 引入antd-mobile/es/global, webpack模式还得按照官网12的方式走 才有样式

@miracles1919
Copy link
Contributor

这个应该是 nextjs 的问题,使用 next: 13.4.6 或最新的版本无需 import antd-mobile/es/global


经过我的测试发现(next: 13.4.5 ),page 如果不使用 css module 或 import css,就不会生成 page.css,从而丢失了 mobile 的样式

'use client';

import { Button } from 'antd-mobile';

export default function Home() {
  return (
   <main>
     <Button>btn</Button>
   </main>
  }
)

解决方法1:使用最新版本 nextjs

解决方法2:使用 css module

'use client';

import { Button } from 'antd-mobile';
+ import styles from './page.module.css';

export default function Home() {
  return (
+   <main className={styles.main}>
     <Button>btn</Button>
   </main>
  }
)

解决方法3:在 layout 加载 global 样式

// layout.tsx
+ import 'antd-mobile/es/global';

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang='en'>
      <body className={inter.className}>{children}</body>
    </html>
  );
}

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

No branches or pull requests

7 participants