Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript lit html:连接字符串以使用html``_Javascript_Web Component_Lit Element_Lit Html - Fatal编程技术网

Javascript lit html:连接字符串以使用html``

Javascript lit html:连接字符串以使用html``,javascript,web-component,lit-element,lit-html,Javascript,Web Component,Lit Element,Lit Html,我正在用lit元素编程一个web组件,我想知道如何在一个变量中呈现一个字符串,这个变量仍然有完整的@click监听器,使用html“processor”而不是纯字符串 someFunction() { var someString=`<button @click="alert('hi')">blah</button>`; return html([someString]); } render() { return this.someFunction

我正在用lit元素编程一个web组件,我想知道如何在一个变量中呈现一个字符串,这个变量仍然有完整的@click监听器,使用html“processor”而不是纯字符串

someFunction() {
    var someString=`<button @click="alert('hi')">blah</button>`;
    return html([someString]);
}
render() {
    return this.someFunction();
}
someFunction(){
var someString=`blah`;
返回html([someString]);
}
render(){
返回这个.someFunction();
}
不起作用,而

someFunction() {
    return html`<button @click="alert('hi')">blah</button>`
}
render() {
    return this.someFunction();
}
someFunction(){
返回html`blah`
}
render(){
返回这个.someFunction();
}
工作


谢谢你的帮助

这是不可能的。这是两个标签模板文字和函数调用根本不同的东西:

标记模板文字 将
html
作为函数调用
html([someString])
将使用数组作为其第一个参数调用该函数

作为一种语言功能,浏览器需要从普通函数中理解标记模板。它发生在词汇/语法层面,因此标记函数必须且必须后跟反勾(`)字符

总之,标记函数
html
如果没有backtick就不能存在。您可以在此处阅读更多内容。

您是否尝试过使用
返回html(someString)
html`<button @click="alert('hi')">blah</button>`
const someString = `<button @click="alert('hi')">blah</button>`;
html([someString]);