Javascript 简单客户端渲染不起作用
我试图在智能组件中触发Javascript 简单客户端渲染不起作用,javascript,marko,Javascript,Marko,我试图在智能组件中触发onMount时渲染组件。服务器似乎正确地呈现了组件,但是当客户端上触发了未呈现的onMount时,我得到了一个简单的未定义的 const button = require('src/client/components/a-button'); console.log(button); // --> { path: '/home/karl/dev/instanty/node/src/client/components/a-button.marko.js', _: [Ge
onMount
时渲染组件。服务器似乎正确地呈现了组件,但是当客户端上触发了未呈现的onMount
时,我得到了一个简单的未定义的
const button = require('src/client/components/a-button');
console.log(button); // --> { path: '/home/karl/dev/instanty/node/src/client/components/a-button.marko.js', _: [Getter/Setter], '$__shouldBuffer': true, meta: {} }
const htmlServer = button.renderToString({ label: 'Click me!' }); // <-- works
console.log(htmlServer);
module.exports = class {
onMount() {
console.log(button); // --> Template {path: undefined, meta: undefined, _: function}
const html = button.renderToString({ label: 'Click me!' }); // <-- does not work
console.log(html);
}
//... more code
}
const-button=require('src/client/components/a-button');
console.log(按钮);//-->{path:'/home/karl/dev/instanty/node/src/client/components/a-button.marko.js',[Getter/Setter],“$\u shouldBuffer”:true,meta:{}
const htmlServer=button.renderString({label:'Click me!'});//模板{path:undefined,meta:undefined,{uu:function}
const html=button.renderString({label:'Click me!'});// 这是由于MarkoV4中的限制。Marko v4设计用于在浏览器中呈现[V]DOM节点,而不是HTML字符串。如果确实需要HTML字符串,则需要使用类似以下代码从DOM节点获取HTML字符串:
const html = button.renderSync({ label: 'Click me!' })
.getNode().firstChild.outerHTML;
注意:getNode()
返回一个DocumentFragment
节点,因为UI组件可能呈现多个顶级HTML元素。我们在上面的代码中使用firstChild
从DocumentFragment
中获取第一个节点,并假设这是您感兴趣的HTML元素节点
话虽如此,我们应该更新文档以明确这一点(或者实现toString()
,即使它确实不需要)。您能console.log(按钮)
吗?常量是块作用域,因此按钮
可能在onMount
作用域中未定义。@Razzildinho已更新@Razzildinho它似乎没有在客户端找到模块。