之前课程中老师用的babel的版本比较低,我在学习时安装的babel版本较高,因此每当使用class语法或decorator修饰器时都会出现一些报错的情况!

ERROR in ./src/index.js 4:1

Module parse failed: Unexpected character '@' (4:1)

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

| class Store {

> @observable array = [];

| @observable obj = {};

| @observable map = new Map();

在网上搜索了各种资料后,尝试修正了package.json和webpack.config.js文件的写法

  1. //package.json
  2. {
  3. "name": "mobx-test",
  4. "version": "1.0.0",
  5. "description": "",
  6. "main": "index.js",
  7. "scripts": {
  8. "start": "webpack -w",
  9. "test": "echo \"Error: no test specified\" && exit 1"
  10. },
  11. "keywords": [],
  12. "author": "",
  13. "license": "ISC",
  14. "devDependencies": {
  15. "@babel/core": "^7.7.7",
  16. "@babel/plugin-proposal-class-properties": "^7.7.4",
  17. "@babel/plugin-proposal-decorators": "^7.7.4",
  18. "@babel/preset-env": "^7.7.7",
  19. "babel-loader": "^7.1.5",
  20. "babel-preset-env": "^1.7.0",
  21. "webpack": "^4.41.3",
  22. "webpack-cli": "^3.3.10"
  23. },
  24. "dependencies": {
  25. "mobx": "^5.15.1"
  26. }
  27. } 
  1. //webpack.config.js
  2. const path = require('path');
  3.  
  4. module.exports = {
  5. mode:'development',
  6. entry:path.resolve(__dirname,'src/index.js'),
  7. output:{
  8. path:path.resolve(__dirname,'dist'),
  9. filename:'main.js'
  10. },
  11. module: {
  12. rules:[{
  13. test: /\.js$/,
  14. exclude: /node_modules/,
  15. use:{
  16. loader:'babel-loader',
  17. options: {
  18. "presets": [
  19. [
  20. "@babel/preset-env",
  21. {"useBuiltIns":"entry"}
  22. ]
  23. ],
  24. "plugins": [
  25. ["@babel/plugin-proposal-decorators", {"legacy": true}],
  26. ["@babel/plugin-proposal-class-properties", {"loose": true}]
  27. ]
  28. }
  29. }
  30. }]
  31. },
  32. devtool:'inline-source-map'
  33. } 

不再提示上面的错误,报了另一个错误:

ERROR in ./src/index.js

Module build failed (from ./node_modules/babel-loader/lib/index.js):

Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure you have a compatible version of @babel/core, it is likely that something in your build process is loading the wrong version. Inspect the stack trace of this error to look for the first entry that doesn't mention "@babel/core" or "babel-core" to see what is calling Babel.

按照错误提示信息,重新安装了低版本babel-core,在命令行中执行:

npm install babel-core@^7.0.0-bridge.0 @babel/core -D

安装好后,重新执行npm start启动项目,就不再报错了~

但是仍然有一个警告⚠️:

WARNING: We noticed you're using the `useBuiltIns` option without declaring a core-js version. Currently, we assume version 2.x when no version is passed. Since this default version will likely change in future versions of Babel, we recommend explicitly setting the core-js version you are using via the `corejs` option.

You should also be sure that the version you pass to the `corejs` option matches the version specified in your `package.json`'s `dependencies` section. If it doesn't, you need to run one of the following commands:

npm install --save core-js@2    npm install --save core-js@3

yarn add core-js@2              yarn add core-js@3

在以后项目中遇到问题时需要解决,在学习阶段暂时忽略一阵子。。。

mobx中使用class语法或decorator修饰器时报错的更多相关文章

  1. mobx学习笔记03——mobx基础语法(decorator修饰器)

    在声明阶段实现类与类成员注解的一种语法. function log(target){ const desc = Object.getOwnPropertyDescriotors(target.prot ...

  2. js基石之---es7的decorator修饰器

    es7的decorator修饰器 装饰器(Decorator)是一种与类(class)相关的语法,用来注释或修改类和类方法. decorator就是给类添加或修改类的变量与方法的. 装饰器是一种函数, ...

  3. 19.Decorator修饰器

    Decorator 修饰器 类的修饰 许多面向对象的语言都有修饰器(Decorator)函数,用来修改类的行为.目前,有一个提案将这项功能,引入了 ECMAScript. @testable clas ...

  4. ES6(Decorator(修饰器))

    Decorator(修饰器) 1.基本概念 函数用来修改 类 的行为 1.Decorator 是一个函数 2.通过Decorator(修饰器)能修改 类 的行为(扩展 类 的功能)3.Decorato ...

  5. es6 Decorator修饰器

    1.类的修饰: 修饰器(Decorator)函数,用来修改类的行为.修饰器是一个对类进行处理的函数.修饰器函数的第一个参数,就是所要修饰的目标类. @testable class MyTestable ...

  6. python decorator 修饰器

    decorator 就是给函数加一层皮,好用! 1 from time import ctime 2 3 def deco(func): 4 def wrappedFunc(*args, **kwar ...

  7. ES6 Decorator 修饰器

    目的:  修改类的一种方法,修饰器是一个函数 编译: 安装 babel-plugin-transform-decortators-legacy .babelrd      plugins: [&quo ...

  8. 21.Decorator修饰器

    1.类的修饰 2.方法的修饰 3.为什么修饰器不能用于函数? 4.core-decorators.js 5.使用修饰器实现自动发布事件 6.Mixin 7.Trait 8.Babel转码器的支持

  9. eclipse中对Hadoop项目进行mvn clean install时报错的处理

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean) ...

随机推荐

  1. Spring 讲解(五)

    Spring 中使用 xml 配置开发和使用注解开发案例 1.Spring 中使用 xml 配置开发案例 接口 public interface UserDao { void add(User use ...

  2. HttpClient类详解

    文章链接:https://blog.csdn.net/justry_deng/article/details/81042379 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了 ...

  3. demo_service

    <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit ...

  4. OC学习篇之---数组对象的引用计数问题和自动释放池的概念

    之前一片文章中我们介绍了OC中的两个关键字@property和@synthesize的使用的使用: http://blog.csdn.net/jiangwei0910410003/article/de ...

  5. k8s pod,pvc,pv无法删除问题

    注意步骤: 一般删除步骤为:先删pod再删pvc最后删pv 但是遇到pv始终处于“Terminating”状态,而且delete不掉 pod一直删不掉 [root@hadoop01 nacos-k8s ...

  6. delphi 监控文件系统

    elphi 监控文件系统 你是否想为你的Windows加上一双眼睛,察看使用者在机器上所做的各种操作(例如建立.删除文件:改变文件或目录名字)呢? 这里介绍一种利用Windows未公开函数实现这个功能 ...

  7. Linux内核学习--写一个c程序,并在内核中编译,运行

    20140506 今天开始学习伟大的开源代表作:Linux内核.之前的工作流于几个简单命令的应用,因着对Android操作系统的情愫,“忍不住”跟随陈利君老师的步伐,开启OS内核之旅.学习路径之一是直 ...

  8. React的contextType的使用方法简介

    上一篇介绍了Context的使用方法.但是Context会让组件变得不纯粹,因为依赖了全局变量.所以这决定了Context一般不会大规模的使用.所以一般在一个组件中使用一个Context就好. 由于C ...

  9. Python-字符串str和json格式的转换

    str转json str转换为json格式,前提一定需要保证这个str的格式和json是一致的,即左边最外层是大括号,右边的最外层是大括号.如果不一致,推荐用正则进行拆分至和json格式一致1. 通过 ...

  10. (转)使用OpenGL ES显示图像

    编写:jdneo - 原文:http://developer.android.com/training/graphics/opengl/index.html 转:http://hukai.me/and ...