Maison  >  Questions et réponses  >  le corps du texte

fonctions de flèche es6 et ceci

 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

**Veuillez demander :

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

répondre à tous(2)je répondrai

  • 巴扎黑

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

    peut être modifié en

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

    répondre
    0
  • PHP中文网

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

    Le maléfique ES2015 !!!
    Laissez-moi vous le raconter.

    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();

    En savoir plus sur les bases, ce n'est en fait pas difficile à comprendre

    répondre
    0
  • Annulerrépondre