【问题标题】:Adding React Template to React将 React 模板添加到 React
【发布时间】:2022-01-21 16:39:53
【问题描述】:

我正在尝试使用 react-template。我正在使用 grunt、browserify 和 react-templateify。代码编译没有错误,但没有显示任何内容。代码很简单。

使用 react-template 编译的 hello.rt.js 代码

import React from 'react';
var helloRT = function () {
    return React.createElement('div', {}, 'Hola Rt');
};

export default helloRT;

Rtexample.js

import React, { Component }  from 'react';
import helloRT from './hello.rt.js';



class Rtexample extends Component {
  
    render(){
      return helloRT;
   }
}



export default Rtexample;

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    hello.rt.js 中使用完整函数而不是匿名函数解决了我的问题。见:

    /* old */
    var helloRT = function () { /* ... */ }
    
    /* new */
    function helloRT() { /* ... */ }
    

    【讨论】:

    • 如果这对您有用,请务必接受!
    【解决方案2】:

    你可以像这样配置 Gruntfile.js:

    reactTemplates: {
            dist: {
              src: ['./src/components/**/*.rt'], //glob patterns of files to be processed
              options: {
                modules: 'es6',  //possible values: (amd|commonjs|es6|typescript|none)
                format: 'stylish' //possible values: (stylish|json)
              }
            }
          },
    

    es6 很重要。这解决了问题。像这样编译rt文件:

    import * as React from 'react';
    import * as _ from 'lodash';
    export default function () {
        return React.createElement('div', {}, 'Hola Rt cambiada');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 2018-09-19
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      相关资源
      最近更新 更多