gpt4 book ai didi

reactjs - React js 相当于 ng-include

转载 作者:行者123 更新时间:2023-12-03 13:35:40 25 4
gpt4 key购买 nike

我正在使用Reactjs。我想在我的 View 中包含部分 html 。类似于 ng-include 的东西。最好的方法是什么?

<div>
<ng-include src="myPageUrl"></ng-include>
</div>

我尝试了以下方法,但没有成功 -

'Uncaught Error: Invariant Violation: traverseAllChildren(...): 
Encountered an invalid child;
DOM elements are not valid children of React components'

我的代码如下所示(基于 http://benalpert.com/react/tips/initial-ajax.html )

var PageContainer = React.createClass({
getInitialState: function() {
return {
page: "loading page ..."
};
},
componentDidMount : function(){
$.get(url, function(result) {
var data = result;
this.setState({
page: data
});
}.bind(this));
},
render: function () {
return (
<div id="page" className="PageContainer">
{this.state.page}
</div>
);
}

});

最佳答案

根据React documentation :

As a last resort, you always have the ability to insert raw HTML.

<div dangerouslySetInnerHTML={{__html: 'First &middot; Second'}} />

所以对你来说可能是:

render: function () {
return (
<div id="page" className="PageContainer"
dangerouslySetInnerHTML={{__html: this.state.page}}
>
</div>
);

关于reactjs - React js 相当于 ng-include,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24634765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com