首页 index.html <body> <div id="hello"></div> <script src="./lib/layui/layui.js"></script> <script> layui.use('layer', function(){ var $ = layui.$; // 加载hello组件(会自动绑定组件对应的dom上) $("#hello").load("./components/hello.html"); }); </script> </body>
组件 hello.html <script id="demo" type="text/html"> <h3>{{ d.title }}</h3> </script> <script> var data = { //数据 "title": "helloword" } var getTpl = demo.innerHTML , view = document.getElementById('hello'); layui.use('laytpl', function () { var laytpl = layui.laytpl; laytpl(getTpl).render(data, function (html) { view.innerHTML = html; }); }) </script>