简体   繁体   English

[React.js] 尝试导入错误:'./registerServiceWorker' 不包含默认导出(导入为 'registerServiceWorker')

[英][React.js]Attempted import error: './registerServiceWorker' does not contain a default export (imported as 'registerServiceWorker')

I am trying to create a react app fetching pokemon API, but I've encountered the following issue that says Attempted import error: './registerServiceWorker' does not contain a default export (imported as 'registerServiceWorker').我正在尝试创建一个获取 pokemon API 的 React 应用程序,但我遇到了以下问题,表示尝试导入错误:“./registerServiceWorker”不包含默认导出(导入为“registerServiceWorker”)。 but I do have a export default App;但我确实有一个export default App; in my App.js file, here's my following content, not too sure what is the issue, help will be massively appreciated.在我的 App.js 文件中,这是我的以下内容,不太确定是什么问题,将非常感谢帮助。

App.js应用程序.js

import React, { Component } from 'react';
import './styles/App.css';


class App extends Component {
  render() {
    return (
      <div className="App">
        hellohello
      </div>
    );
  }
}


export default App;

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './components/App';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();



const isLocalhost = Boolean(
  window.location.hostname === 'localhost' ||
    // [::1] is the IPv6 localhost address.
    window.location.hostname === '[::1]' ||
    // 127.0.0.0/8 are considered localhost for IPv4.
    window.location.hostname.match(
      /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
    )
);

export function register(config) {
  if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {

    const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
    if (publicUrl.origin !== window.location.origin) {

      return;
    }

    window.addEventListener('load', () => {
      const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;

      if (isLocalhost) {
        checkValidServiceWorker(swUrl, config);


        navigator.serviceWorker.ready.then(() => {
          console.log(
            'This web app is being served cache-first by a service ' +
              'worker.'
          );
        });
      } else {
        registerValidSW(swUrl, config);
      }
    });
  }
}

function registerValidSW(swUrl, config) {
  navigator.serviceWorker
    .register(swUrl)
    .then(registration => {
      registration.onupdatefound = () => {
        const installingWorker = registration.installing;
        if (installingWorker == null) {
          return;
        }
        installingWorker.onstatechange = () => {
          if (installingWorker.state === 'installed') {
            if (navigator.serviceWorker.controller) {

              console.log(
                'New content is available and will be used when all ' +
                  'tabs for this page are closed.'
              );


              if (config && config.onUpdate) {
                config.onUpdate(registration);
              }
            } else {

              console.log('Content is cached for offline use.');


              if (config && config.onSuccess) {
                config.onSuccess(registration);
              }
            }
          }
        };
      };
    })
    .catch(error => {
      console.error('Error during service worker registration:', error);
    });
}

function checkValidServiceWorker(swUrl, config) {
  fetch(swUrl, {
    headers: { 'Service-Worker': 'script' }
  })
    .then(response => {
      const contentType = response.headers.get('content-type');
      if (
        response.status === 404 ||
        (contentType != null && contentType.indexOf('javascript') === -1)
      ) {
        navigator.serviceWorker.ready.then(registration => {
          registration.unregister().then(() => {
            window.location.reload();
          });
        });
      } else {
        registerValidSW(swUrl, config);
      }
    })
    .catch(() => {
      console.log(
        'No internet connection found. App is running in offline mode.'
      );
    });
}

export function unregister() {
  if ('serviceWorker' in navigator) {
    navigator.serviceWorker.ready.then(registration => {
      registration.unregister();
    });
  }
}

Your module does not have a default export.您的模块没有默认导出。 It has a named export register .它有一个命名的导出register

You can import it using the alias registerServiceWorker with:您可以使用别名registerServiceWorker导入它:

import { register as registerServiceWorker } from './registerServiceWorker';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React JS 尝试导入错误:&#39;./Pathfinder/Pathfinder&#39; 不包含默认导出(导入为 &#39;Pathfinder&#39;) - React JS Attempted import error: './Pathfinder/Pathfinder' does not contain a default export (imported as 'Pathfinder') 尝试导入错误:“./components”不包含默认导出(导入为“App”) - Attempted import error: './components' does not contain a default export (imported as 'App') 尝试导入错误:“./movieReducer”不包含默认导出(导入为“movieReducer”) - Attempted import error: './movieReducer' does not contain a default export (imported as 'movieReducer') ./src/index.js 尝试导入错误:“./App.js”不包含默认导出(导入为“App”) - ./src/index.js Attempted import error: './App.js' does not contain a default export (imported as 'App') 尝试导入错误:“react-table”不包含默认导出(导入为“ReactTable”) - Attempted import error: 'react-table' does not contain a default export (imported as 'ReactTable') React-Redux:尝试导入错误:“./components/Score”不包含默认导出(导入为“Score”) - React-Redux: Attempted import error: './components/Score' does not contain a default export (imported as 'Score') registerServiceWorker在React JS中做了什么? - What does registerServiceWorker do in React JS? 尝试导入错误:“uuid”在 React 中不包含默认导出(导入为“uuid”) - Attempted import error: 'uuid' does not contain a default export (imported as 'uuid') In React index.js 尝试导入错误:“./App”不包含默认导出(导入为“App”) - index.js attempted import error: './App' does not contain a default export (imported as 'App') ./src/components/styles.js 尝试导入错误:&#39;@material-ui/core/styles&#39; 不包含默认导出(作为 &#39;makeStyles&#39; 导入) - ./src/components/styles.js Attempted import error: '@material-ui/core/styles' does not contain a default export (imported as 'makeStyles')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM