简体   繁体   English

如何解决反应中的服务人员错误?

[英]How to resolve service worker error in react?

I am getting a service worker error while running my react app.我在运行我的 react 应用程序时遇到了 service worker 错误。 Can you please help me why it's getting the error.你能帮我解释一下为什么会出错。 Screenshot link is given below,截图链接如下,

Servier worker error message Servier 工作人员错误消息

import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, applyMiddleware, compose } from 'redux';
import rootReducer from './reducers/rootReducer';
import {Provider} from 'react-redux';
import thunk from 'redux-thunk';
import config from './config/index'
import App from './App';
import './index.scss'
import * as serviceWorker from './serviceWorker';

const store = createStore(rootReducer,
  compose(
    applyMiddleware(thunk.withExtraArgument({}))
  )
);

ReactDOM.render(<Provider store={store}><App /></Provider>, document.getElementById('root'));
serviceWorker();

It's a simple mistake =)这是一个简单的错误=)

Look at this line:看看这一行:

import * as serviceWorker from './serviceWorker';

and then look here:然后看这里:

serviceWorker();

On the first line of code you're importing the whole module into your file's scope as 'serviceWorker'.在第一行代码中,您将整个模块作为“serviceWorker”导入文件的 scope。 On the second line you're trying to call that module.在第二行,您尝试调用该模块。 But you cannot call a module, it's not allowed.但是你不能调用一个模块,这是不允许的。 It's also what the error message says right at the top of your screenshot.这也是屏幕截图顶部的错误消息。

I think what you actually should be doing is something like this:我认为你实际上应该做的是这样的:

import { register } from './serviceWorker';
...
...
register();

or something similar.或类似的东西。 It might also be registerServiceWorker or something like that.它也可能是registerServiceWorker或类似的东西。 Look at the ./serviceWorker.js file and find out.查看./serviceWorker.js文件并找出答案。

One thing to note: I would seriously consider _not using_ Service Workers in your project until you know exactly how they work, what they are etc. It is SUPER EASY to cause a lot of problems by using Service Workers without really knowing them.需要注意的一件事:我会认真考虑在您的项目中_不使用_ Service Worker ,直到您确切知道它们是如何工作的,它们是什么等。在不真正了解它们的情况下使用 Service Worker 很容易引起很多问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM