当前位置: 首页 > 知识库问答 >
问题:

为什么registerServiceWorker.js没有“导出默认registerServiceWorker;”最后呢?

葛海阳
2023-03-14

App.js的末尾,我们将

export default App;

App声明为App.js的默认导出。

然后当我们将这个文件导入到index.js中时,我们简单地将

//Import default App from file App within this directory.
import App from './App';

registerServiceWorker也以这种方式导入。

import registerServiceWorker from './registerServiceWorker';

但是,寄存器ServiceWorker.js中没有包含导出默认寄存器ServiceWorker语句。

我有两个问题-

这是如何工作,而没有一个导出默认寄存器ServiceWorker;语句中的寄存器ServiceWorker.js

而且还

为什么registerServiceWorker从一开始就没有大写?如本SO答案所述,它是内置组件吗?

以下是registerServiceWorker.js,供参考:

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

export default function register() {
  if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
    // The URL constructor is available in all browsers that support SW.
    const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
    if (publicUrl.origin !== window.location.origin) {
      // Our service worker won't work if PUBLIC_URL is on a different origin
      // from what our page is served on. This might happen if a CDN is used to
      // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374
      return;
    }

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

      if (isLocalhost) {
        // This is running on localhost. Lets check if a service worker still exists or not.
        checkValidServiceWorker(swUrl);

        // Add some additional logging to localhost, pointing developers to the
        // service worker/PWA documentation.
        navigator.serviceWorker.ready.then(() => {
          console.log(
            'This web app is being served cache-first by a service ' +
              'worker. To learn more, visit https://example'
          );
        });
      } else {
        // Is not local host. Just register service worker
        registerValidSW(swUrl);
      }
    });
  }
}

function registerValidSW(swUrl) {
  navigator.serviceWorker
    .register(swUrl)
    .then(registration => {
      registration.onupdatefound = () => {
        const installingWorker = registration.installing;
        installingWorker.onstatechange = () => {
          if (installingWorker.state === 'installed') {
            if (navigator.serviceWorker.controller) {
              // At this point, the old content will have been purged and
              // the fresh content will have been added to the cache.
              // It's the perfect time to display a "New content is
              // available; please refresh." message in your web app.
              console.log('New content is available; please refresh.');
            } else {
              // At this point, everything has been precached.
              // It's the perfect time to display a
              // "Content is cached for offline use." message.
              console.log('Content is cached for offline use.');
            }
          }
        };
      };
    })
    .catch(error => {
      console.error('Error during service worker registration:', error);
    });
}

function checkValidServiceWorker(swUrl) {
  // Check if the service worker can be found. If it can't reload the page.
  fetch(swUrl)
    .then(response => {
      // Ensure service worker exists, and that we really are getting a JS file.
      if (
        response.status === 404 ||
        response.headers.get('content-type').indexOf('javascript') === -1
      ) {
        // No service worker found. Probably a different app. Reload the page.
        navigator.serviceWorker.ready.then(registration => {
          registration.unregister().then(() => {
            window.location.reload();
          });
        });
      } else {
        // Service worker found. Proceed as normal.
        registerValidSW(swUrl);
      }
    })
    .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();
    });
  }
}

共有1个答案

白君之
2023-03-14
  1. 因为“default”关键字。这意味着它是唯一的导出,因此您可以只使用类名。

删除“默认”关键字,保存,你会得到一个错误。因为它可以有多个出口。唯一的解决办法是:

 import * as registerServiceWorker from './serviceWorker';

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

希望这有帮助。

 类似资料:
  • Java中的本质上是不可靠的。具体地说,我对接口的最大问题是,它需要一个不定义方法本身的方法行为。因此,如果遍历一个列表,您必须使用反射来访问它定义的行为。然而,在Java8中,我们现在有了默认方法,现在我问为什么在中没有默认的方法。 我理解为什么接口不能默认对象方法,但是,这是一个明确的设计决定,所以可以做出例外。 我有点想弃用并将其内部代码更改为类似以下内容: 然后继续使用使作为中的默认方法的

  • 问题内容: 我想问一下最后一句话的意思和作用(导出默认的HelloWorld;),但是我找不到关于它的任何教程。 问题答案: 出口喜欢和进口,比如是一部分ES6模块系统。 模块是一个独立的单元,可以使用将资产暴露给其他模块,并使用来从其他模块获取资产。 在您的代码中: 在ES6中,有两种导出: 命名出口 -例如,是名称为的命名出口。可以使用导入命名模块。在这种情况下,导入的名称应与导出的名称相同。

  • 如果我有一个struct Foo和一个struct Bar: 如果我初始化一个条并打印正确得到的值: 但是现在如果我声明这样的构造函数: 我失去了Bar::foo的默认构造,程序输出了32764 0 5! 为什么我不得不像这样无声地初始化每个成员变量: 只要我声明一个构造函数?在这种情况下,为什么默认构造不起作用?

  • 问题内容: 档案:SafeString.js 我从未见过。是否有任何等效的东西更容易理解? 问题答案: 它是ES6模块系统的一部分,该文档中还有一个有用的示例: 如果模块定义了默认导出: 那么您可以通过省略花括号来导入默认导出: 更新: 自2015年6月,该模块系统中定义§15.2和在特定语法中定义§15.2.3ECMAScript的2015规范的。

  • 文件:safeString.js 我以前从未见过。对于,是否有更容易理解的等效内容?

  • 我想问最后一句是什么意思和做(导出默认的HelloWorld;),但我找不到关于它的任何教程。