我能用反应框架使用帕格(ex-jade)吗?

时间:2016-06-18 09:53:46

标签: javascript node.js reactjs pug

我已经阅读了一些pug文档。它说我必须先安装哈巴狗,我已经完成了。然后我必须在我的js文件中要求哈巴狗。 但我不知道在我的反应文件中编写pug文件的编译在哪里?在反应框架中使用哈巴狗的正确步骤是什么? 谢谢!我真的很感激任何帮助。 这是我的反应中的一个组成部分,我想用哈巴狗来渲染它。

 import React from 'react';
 import Sidebar from './Sidebar';
 import Header from './header/Header';
 import {tokenverify} from '../../utils/helpers';
 import pug from 'pug';

 class Home extends React.Component {
   componentDidMount() {
     const token = localStorage.getItem('token')
     tokenverify(token)
     .catch((res) => {
       this.props.history.push('/')
     })
   }

   render() {
     return(
       <div className="main-container">
         <div className="col-md-1">
           <Sidebar history={this.props.history} username={this.props.params.username}/>
         </div>
         <div className="col-md-11">
           <div className="row">
             <Header history={this.props.history} username={this.props.params.username} />
           </div>
           <div className="row">
             {this.props.children}
           </div>
         </div>
       </div>
     )
   }
 }

 export default Home

2 个答案:

答案 0 :(得分:4)

我发现这个项目处于开发的早期阶段:https://github.com/bluewings/pug-as-jsx-loader

我喜欢它,因为它让我把我的哑(表象)反应组件写成了哈巴狗模板。

它目前支持的唯一JSX功能是迭代和条件if。这似乎足以写出大部分愚蠢的组件。

以下是使用它的步骤

<强> 1。安装pug-as-jsx-loader

npm install pug-as-jsx-loader --save-dev

如果您使用的是create-react-app

,则必须弹出下一步

<强> 2。告诉webpack如何处理哈巴狗模板。

在你的webpack.config.dev.js中,

{ test: /\.pug$/, use: [require.resolve('babel-loader'), require.resolve('pug-as-jsx-loader')] },

第3。在组件中导入pug模板

import myTemplate from './mycomponent.pug'

<强> 4。从渲染函数返回已编译的模板

const MyComponent = ({someProperty, someOtherProperty})=> {
  return myTemplate.call({}, {
    someProperty,
    someOtherProperty
  });
};

<强> 5。定义一个哈巴狗来渲染组件

#my-element
  ul.my-list
    li(key='{something.id}', @repeat='something as someProperty')
      div(className='planet')  {something.name}
      div(className='vehicle')   {something.type}
      div(className='overview') {something.cost} 
      div(className='cancel', onClick='{()=> someOtherProperty(something)}')
        div(className='no-mobile fa fa-remove')

阅读我的经历:https://medium.com/p/7610967954a

答案 1 :(得分:0)

使用Pug,您有两个选择:将模板渲染为HTML字符串,立即传递数据对象或将模板渲染为有效的javascript函数,该函数在传递数据对象时输出html。

当使用pug(单独)和动态数据时,显然选择编译为函数,以便可以在客户端上应用数据。

但是,React实际上不会消耗或发送给客户端html。 如果您阅读了JSX的解释,您将看到它只是HTML-likealike语法糖被编译为javascript函数,以编程方式创建DOM节点(对于React处理差异和更新页面的方式至关重要)。目前,即使在客户端上,Pug也会输出一个HTML字符串。因此,我们能够使用它的唯一方法是 dangerouslySetInnerHTML如下:

&#13;
&#13;
//from https://runkit.io/qm3ster/58a9039e0ef2940014a4425b/branches/master?name=test&pug=div%20Wow%3A%20%23%7Ba%7D%23%7Bb%7D
function pug_escape(e){var a=""+e,t=pug_match_html.exec(a);if(!t)return e;var r,c,n,s="";for(r=t.index,c=0;r<a.length;r++){switch(a.charCodeAt(r)){case 34:n="&quot;";break;case 38:n="&amp;";break;case 60:n="&lt;";break;case 62:n="&gt;";break;default:continue}c!==r&&(s+=a.substring(c,r)),c=r+1,s+=n}return c!==r?s+a.substring(c,r):s}
var pug_match_html=/["&<>]/;
function pug_rethrow(n,e,r,t){if(!(n instanceof Error))throw n;if(!("undefined"==typeof window&&e||t))throw n.message+=" on line "+r,n;try{t=t||require("fs").readFileSync(e,"utf8")}catch(e){pug_rethrow(n,null,r)}var i=3,a=t.split("\n"),o=Math.max(r-i,0),h=Math.min(a.length,r+i),i=a.slice(o,h).map(function(n,e){var t=e+o+1;return(t==r?"  > ":"    ")+t+"| "+n}).join("\n");throw n.path=e,n.message=(e||"Pug")+":"+r+"\n"+i+"\n\n"+n.message,n}function test(locals) {var pug_html = "", pug_mixins = {}, pug_interp;var pug_debug_filename, pug_debug_line;try {;var locals_for_with = (locals || {});(function (a, b) {;pug_debug_line = 1;
pug_html = pug_html + "\u003Cdiv\u003E";
;pug_debug_line = 1;
pug_html = pug_html + "Wow: ";
;pug_debug_line = 1;
pug_html = pug_html + (pug_escape(null == (pug_interp = a) ? "" : pug_interp));
;pug_debug_line = 1;
pug_html = pug_html + (pug_escape(null == (pug_interp = b) ? "" : pug_interp)) + "\u003C\u002Fdiv\u003E";}.call(this,"a" in locals_for_with?locals_for_with.a:typeof a!=="undefined"?a:undefined,"b" in locals_for_with?locals_for_with.b:typeof b!=="undefined"?b:undefined));} catch (err) {pug_rethrow(err, pug_debug_filename, pug_debug_line);};return pug_html;}
// pug source: "div Wow: #{a}#{b}"
// this would obviously be much shorter if you include pug-runtime globally in your application

function createMarkup(a,b) {
  return {__html: test({a:a,b:b})};
}

function MyComponent(props) {
  return <div dangerouslySetInnerHTML={createMarkup(props.a, props.b)}/>;
}

ReactDOM.render(
  <MyComponent a="banana" b="&patata"/>,
  document.getElementById('root')
)
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id=root />
&#13;
&#13;
&#13;

或者,尝试将jade或pug语法直接转换为反应,例如pug-react-compilerbabel-plugin-transform-pug-to-react。似乎他们解决了包括在哈巴狗模板内部的进一步反应组件,这可能是他们可能有怪癖的理想权衡。