Nan*_*ina 10 javascript node.js reactjs 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
Run Code Online (Sandbox Code Playgroud)
我在其开发的早期阶段发现了这个项目:https : //github.com/bluewings/pug-as-jsx-loader。
我喜欢它,因为它让我可以将我的愚蠢(展示性)反应组件编写为 pug 模板。
它目前支持的唯一 JSX 功能是迭代和条件 if。这似乎足以编写大多数愚蠢的组件。
以下是使用步骤
1.安装pug-as-jsx-loader
npm install pug-as-jsx-loader --save-dev
Run Code Online (Sandbox Code Playgroud)
对于下一步,如果您使用的是 create-react-app,则必须弹出
2. 告诉 webpack 如何处理 pug 模板。
在你的 webpack.config.dev.js 中,
{ test: /\.pug$/, use: [require.resolve('babel-loader'), require.resolve('pug-as-jsx-loader')] },
Run Code Online (Sandbox Code Playgroud)
3. 在你的组件中导入 pug 模板
import myTemplate from './mycomponent.pug'
Run Code Online (Sandbox Code Playgroud)
4.从render函数返回编译模板
const MyComponent = ({someProperty, someOtherProperty})=> {
return myTemplate.call({}, {
someProperty,
someOtherProperty
});
};
Run Code Online (Sandbox Code Playgroud)
5. 定义一个 pug 来渲染组件
#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')
Run Code Online (Sandbox Code Playgroud)
阅读我的经验:https : //medium.com/p/7610967954a
使用 Pug,您有两个选择:将模板渲染为 HTML 字符串,立即传递数据对象,或者将模板渲染为高效的 javascript 函数,该函数在传递数据对象时输出 html。
当将 pug(alone) 与动态数据一起使用时,选择显然是编译为函数,以便可以在客户端上应用数据。
然而,React 实际上并不使用 html 或将 html 发送到客户端。如果您阅读 JSX 的解释,您会发现它只是类似于 HTML 的语法糖,被编译为以编程方式创建 DOM 节点的 JavaScript 函数(对于 React 处理比较和更新页面的方式至关重要)。Pug 目前,即使在客户端,也会输出 HTML 字符串。因此,我们能够使用它的唯一方法是 angerlySetInnerHTML,如下所示:
//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=""";break;case 38:n="&";break;case 60:n="<";break;case 62:n=">";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')
)Run Code Online (Sandbox Code Playgroud)
<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 />Run Code Online (Sandbox Code Playgroud)
或者,也有人尝试将 jade 或 pug 语法直接翻译为 React,例如pug-react-compiler和babel-plugin-transform-pug-to-react. 看来他们解决了在哈巴狗模板中包含更多反应组件的问题,这对于可能有怪癖的他们来说可能是一个理想的权衡。
| 归档时间: |
|
| 查看次数: |
5700 次 |
| 最近记录: |