Heim  >  Fragen und Antworten  >  Hauptteil

es6 Pfeilfunktionen und das

 const Title=React.createClass({
         getDefaults: ()=> {
             return{
                 title:'hello world'
             }
         },
         render:()=>{

             return <h1>{this.props.title}</h1>
         }
     })
    ReactDOM.render(
            <Title/>,
            document.getElementById('app6')
    )
此种情况下报错:Cannot read property 'props' of undefined

**Bitte fragen Sie:

(1)此种情况下箭头函数和this是否可以一起使用?
(2)如果可以一起使用请问有何种解决方法?**
迷茫迷茫2663 Tage vor683

Antworte allen(2)Ich werde antworten

  • 巴扎黑

    巴扎黑2017-07-05 10:39:12

    可以改成

    render() {
        console.log(this);
    }

    Antwort
    0
  • PHP中文网

    PHP中文网2017-07-05 10:39:12

    万恶的ES2015!!!
    给你翻一下。

    function template(config) {
      var self = this;
      Object.keys(config).forEach(function (key) {
        self[key] = config[key];
      });
    }
    function factory() {
    
    }
    
    factory.create = function (config) {
      return new template(config);
    }
    var instance = factory.create({
      title: 'instance1',
      method: () => {
        console.log(this);
      }
    });
    instance.method();
    function template(config) {
      var self = this;
      Object.keys(config).forEach(function (key) {
        self[key] = config[key];
      });
    }
    function factory() {
    
    }
    
    factory.create = function (config) {
      return new template(config);
    }
    var instance = factory.create({
      title: 'instance1',
      method() {
        console.log(this);
      }
    });
    instance.method();

    基础多看看,其实理解并不难

    Antwort
    0
  • StornierenAntwort