【发布时间】:2019-05-11 15:18:43
【问题描述】:
在 Vue 中你可以有类似的东西。
<!-- index.php -->
<div id="app">
<section>
<main>
<h1>Main</h1>
<some-component></some-component>
</main>
<aside>
<h2>Side</h2>
<another-component></another-component>
</aside>
</section>
</div>
<script>
Vue.component('some-component', { template: '<div>hello - {{ this.$parent.test }}</div>' });
Vue.component('another-component', { template: '<div>world - {{ this.$parent.test }}</div>' });
new Vue({el: '#app', data: () => ({test: 123})});
</script>
这将渲染所有注册的组件,你最终会得到类似的东西
<div id="app">
<section>
<main>
<h1>Main</h1>
<div>hello - 123</div>
</main>
<aside>
<h2>Side</h2>
<div>world - 123</div>
</aside>
</section>
</div>
如何使用 React 完成同样的事情?
我尝试过一些愚蠢的事情,比如
class App extends Component {
render() {
return <div>{this.props.kids}</div>
}
}
if (document.getElementById('app')) {
ReactDOM.render(<App kids={document.getElementById('app').innerHTML)} />, document.getElementById('app'));
}
总体目标是能够拥有普通的 html 模板,然后在需要的地方分散各种 React 组件。
理想情况下,它应该包含在一个类似无渲染的组件中,该组件可以使用上下文 api 提供向下的全局数据。
【问题讨论】:
标签: javascript reactjs templates vue.js