装饰器

作者: coffee1949 | 来源:发表于2019-08-13 18:05 被阅读0次

    https://www.cnblogs.com/lgjc/p/10286829.html
    1.弹出项目配置

    npm run eject
    

    此处注意,若弹出项目配置失败,请先执行以下两行代码(我的项目执行上一句都会报错,所以都会执行)

    1.git add .
    2.git commit -m "init"
    

    2.安装装饰器所需依赖

    npm install --save-dev babel-plugin-transform-decorators-legacy
    

    3.安装应用配置

    npm install @babel/plugin-proposal-decorators
    

    4.安装mobx和mobx-react

    npm install mobx mobx-react -S
    

    5.配置package.json(直接在最下方加上就行)

    "babel"{
       "plugins":[
       [
         "@babel/plugin-proposal-decorators",
         {
             "legacy":true
         }
       ],
       [
         "@babel/plugin-proposal-class-properties",
         {
             "loose":true
         }
         ]
         ],
        "presets":[
        "react-app"
        ]
    },
      
    

    6.环境测试

    1.新建store.js模拟本地数据库

    import {observable,action} from "mobx";
    class Store {
        id = Math.random();
        @observable title="飞飞";
        @observable finished = false;
        @action cTitle =()=>{
            this.title="麦扣扣"
        }
    }
    export  default  new Store()
    

    2.新建App.js进行调用

    import React, { Component } from 'react';
    import Store from "./store";
    import {observer} from "mobx-react"
     
    @observer
    class App extends Component {
     render() {
       return (
         <div>
           this is Index
           <p>{Store.id}</p>
           <p>{Store.title}</p>
           <button onClick={Store.cTitle}>测试</button>
         </div>
       );
     }
    }
    export default App;
    
    报错:Cannot find module 'is-wsl'
    解决:npm i
    

    相关文章

      网友评论

          本文标题:装饰器

          本文链接:https://www.haomeiwen.com/subject/zbuadctx.html