鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ react+webpack+mobx的項目創(chuàng)建

react+webpack+mobx的項目創(chuàng)建

問題描述

在創(chuàng)建項目的過程中,裝飾器語法總是提示不能解析

自己嘗試過哪些方法

在網(wǎng)上搜到很多關(guān)于支持修飾器的配置方法,在即也是了很多,下面是代碼,但總是報錯

相關(guān)代碼

.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]

你期待的結(jié)果是什么?

希望大神可以指導(dǎo)下我的配置是否哪里有問題,或者能指導(dǎo)我重新新建一個react+webpack+mobx項目 從頭開始越詳細越好。。

回答
編輯回答
拮據(jù)

webpack配置文件

var path=require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
var utils = require('./utils');         // 引入獲取文件路徑的函數(shù)
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'
    })]
}
2017年1月15日 01:54
編輯回答
誮惜顏

之前按需引入ui庫css 在.babelrc中配置有問題 在百度參考好多文章都沒解決
最后在package.json中配置好了
@ 裝飾器也配好了
使用 螞蟻金服 npm i antd-mobile@next --save
網(wǎng)址: https://mobile.ant.design/ind...

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

在package.json中配置 找到這個

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

// 配置成下面這個樣子 單引號改為雙引號 沒引號改為雙引號 這時打開頁面 你會發(fā)現(xiàn)配置好了按需加載 "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 來提交數(shù)據(jù) 下載 npm i react-redux -S(是--save的簡寫)

這是Babel 6的一個插件,用于復(fù)制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"
    }
  ]

]

2017年5月2日 19:24