reactjs - Next.js 页面无法路由
问题描述
开发环境
next.js
Typescript
Styled-components
我在构建 next.js 环境中所做的工作
yarn create next-app
yarn add --dev typescript @types/react @types/node
yarn add styled-components yarn add -D @types/styled-components
我跑了上面的四个
我不明白
执行上述命令,搭建next.js的环境。
我在 pages 目录下创建了 user.tsx 并访问了 pages/users,但是我得到了 404 Not Found 错误。为什么?
包.json
{
"name": "next-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "9.4.4",
"react": "16.13.1",
"react-dom": "16.13.1",
"styled-components": "^5.1.1"
},
"devDependencies": {
"@types/node": "^14.0.18",
"@types/react": "^16.9.41",
"@types/styled-components": "^5.1.0",
"typescript": "^3.9.6"
}
}
import React, { FC } from "react";
import styled from "styled-components";
import Sidebar from "../components/atoms/SideBar";
import UserList from "../components/atoms/UserList";
const UsersStyle = styled.div`
display: flex;
height: 100vh;
`;
export const User: FC = () => {
return (
<UsersStyle>
<Sidebar />
<UserList />
</UsersStyle>
);
};
解决方案
尝试将代码更改为默认导出。
import React, { FC } from "react";
import styled from "styled-components";
import Sidebar from "../components/atoms/SideBar";
import UserList from "../components/atoms/UserList";
const UsersStyle = styled.div`
display: flex;
height: 100vh;
`;
const User: FC = () => {
return (
<UsersStyle>
<Sidebar />
<UserList />
</UsersStyle>
);
};
export default User;
推荐阅读
- python - 将加载为二进制字符串的图像转换为 numpy 数组
- powershell - 如何将多个参数传递给 PowerShell Start-Job 以在后台运行进程?
- android - 替换整个数据集时的 RecyclerView 动画
- c# - 使用 Entity Framework Core 的 LEFT JOIN 查询
- makefile - make 模式自动生成依赖
- c++ - word2vec中CBOW和Skipgram梯度的区别?
- regex - 我应该如何使用正则表达式来搜索和替换 url 中的特殊字符?
- mysql - MySQL information_schema 更改 COLLATIONS IS_DEFAULT
- python - matplotlib 底图鼠标事件
- c# - 带有继承的 Web API 版本控制