react+webpack+mobx的项目创建

发布于 2022-09-07 19:41:22 字数 1768 浏览 16 评论 0

问题描述

在创建项目的过程中,装饰器语法总是提示不能解析

自己尝试过哪些方法

在网上搜到很多关于支持修饰器的配置方法,在即也是了很多,下面是代码,但总是报错

相关代码

.babelrc

{
  "presets": ["env","react"],
  "plugins": [
    "transform-decorators-legacy",
    "transform-class-properties"
  ]
}

.package.json

{
  "name": "mobx-demo",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "mobx": "^5.0.3",
    "mobx-react": "^5.2.3",
    "react": "^16.4.1",
    "react-dom": "^16.4.1",
    "react-scripts": "1.1.4",
    "webpack": "^4.16.1",
    "webpack-dev-server": "^3.1.4"
  },
  "scripts": {
    "start": "webpack-dev-server --mode development --config build/webpack.config.js",
    "dev": "webpack --mode development --config build/webpack.config.js",
    "build": "webpack --mode production --config build/webpack.config.js"
  },
  "devDependencies": {
    "babel-loader": "^7.1.5",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-decorators-legacy": "^1.3.5",
    "babel-preset-env": "^1.7.0",
    "babel-preset-mobx": "^1.0.3",
    "html-webpack-plugin": "^3.2.0",
    "webpack-cli": "^3.0.8"
  },
  "plugins": [
    "transform-decorators-legacy",
    "transform-class-properties"
  ]
}

错误代码

ERROR in ./src/entry/main.js 8:4
Module parse failed: Unexpected character '@' (8:4)
You may need an appropriate loader to handle this file type.
| class Todo {
|     // id = Math.random();
>     @observable title;
|     @observable finished = false;
|     constructor(title){
 @ multi (webpack)-dev-server/client?http://localhost:8081 ./src/entry/main.js main[1]

你期待的结果是什么?

希望大神可以指导下我的配置是否哪里有问题,或者能指导我重新新建一个react+webpack+mobx项目 从头开始越详细越好。。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琴流音 2022-09-14 19:41:22

webpack配置文件

var path=require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
var utils = require('./utils');         // 引入获取文件路径的函数
var ROOT = utils.fullPath('../');       // 获取顶层文件的路径
console.log(ROOT)
module.exports={
    entry:'./src/entry/main.js',
    output:{
        path: ROOT + '/dist',
        filename:'bundle.js'
    },
    loader: {
        rules: [{
            test: /\.js[x]?$/,
            loader: 'babel-loader',
            exclude: /node_modules/
        }]
    },
    resolve: {
        extensions: [".js", ".jsx"]
    },
    plugins: [new HtmlWebpackPlugin({
        title: 'mobx-demo222',
        template: './src/entry/index.html',
        inject: 'head'
    })]
}
节枝 2022-09-14 19:41:22

之前按需引入ui库css 在.babelrc中配置有问题 在百度参考好多文章都没解决
最后在package.json中配置好了
@ 装饰器也配好了
使用 蚂蚁金服 npm i antd-mobile@next --save
网址: https://mobile.ant.design/ind...

按需引入antd-mobile的css npm i babel-plugin-import --save

在package.json中配置 找到这个

"babel": { "presets": [ "react-app" ] },

// 配置成下面这个样子 单引号改为双引号 没引号改为双引号 这时打开页面 你会发现配置好了按需加载 "babel": { "presets": [ "react-app" ], "plugins": [ ["import", { "libraryName": "antd-mobile", "style": "css" }] ] },

redux 使用 npm i redux --save
使用 npm i redux-thunk --save 异步处理事件避免store嵌套 redux默认处理同步 异步需要使用redux-thunk中间键

不使用store.subscribe 来提交数据 下载 npm i react-redux -S(是--save的简写)

这是Babel 6的一个插件,用于复制Babel 5中的旧装饰器行为
https://www.npmjs.com/package...
npm i babel-plugin-transform-decorators-legacy --save-dev 修饰器
在package.json 中配置:
"plugins": [

  "transform-decorators-legacy",
  [
    "import",
    {
      "libraryName": "antd-mobile",
      "style": "css"
    }
  ]

]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文