首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >netx.js不能导入组件

netx.js不能导入组件
EN

Stack Overflow用户
提问于 2021-05-25 17:51:55
回答 1查看 46关注 0票数 0

为什么next.js不能导入组件。有效组件/我现在不知道如何修复此错误

代码语言:javascript
运行
复制
import React from "react";
import Title from '../../../common/components/Title'


export default function Fill(props: any) {
  return (
    <React.Fragment>
      <Title></Title>
    </React.Fragment>
  )
}

Title.js

代码语言:javascript
运行
复制
export default function Title(props) {
  return <h1>{ props?.title }</h1>
}

导入组件时出错

代码语言:javascript
运行
复制
error - ../../common/components/Title.js
Module parse failed: Unexpected token (2:9)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| export default function Title(props) {
>   return <h1>{ props?.title }</h1>
| }
|
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-25 18:46:31

解析父文件夹中的路径

next.config.js

代码语言:javascript
运行
复制
const path = require('path');

module.exports = {
  webpack: function (config, { defaultLoaders }) {
    const resolvedBaseUrl = path.resolve(config.context, "../../");
    config.module.rules = [
      ...config.module.rules,
      {
        test: /\.(tsx|ts|js|mjs|jsx)$/,
        include: [resolvedBaseUrl],
        use: defaultLoaders.babel,
        exclude: (excludePath) => {
          return /node_modules/.test(excludePath);
        },
      },
    ];
    return config;
  }
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67693190

复制
相关文章

相似问题

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