gpt4 book ai didi

javascript - 嵌套 polymer 组件内容问题

转载 作者:行者123 更新时间:2023-12-03 06:07:44 25 4
gpt4 key购买 nike

foo.html:

<link rel="import" href="bar.html">
<dom-module id="p-foo">
<template>
<p-bar>
<content select=".myContent"></content>
</p-bar>
</template>
<script>
Polymer( {
is: 'p-foo',
} )
</script>
</dom-module>

bar.html:

<dom-module id="p-bar">
<template>
bar open
<content select=".myContent"></content>
bar closed
</template>
<script>
Polymer( {
is: 'p-bar',
} )
</script>
</dom-module>

演示.html:

<!DOCTYPE html>
<html>
<head>
...
<link rel="import" href="foo.html">
</head>
<body>
<p-foo><div class="myContent"><strong>hello</strong></div></p-foo>
</body>
</html>

预期输出:

bar open
hello
bar closed

我有时会得到什么:

bar open
bar closed
hello

我收到的错误不是 100% 可重现的。它只发生在我刷新页面的一定比例的时间中。看来内容越复杂,发生错误的可能性就越大。

polymer 似乎在 bar 组件完全渲染之前尝试选择 .myContent

最佳答案

  1. 您需要通过调用Polymer()来注册新的自定义元素。 .

  2. 此外,正如注释中所述,您的自定义元素需要包含连字符。例如:<p-foo><p-bar> .

foo.html:

<link rel="import" href="bar.html">
<dom-module id="p-foo">
<template>
<p-bar>
<content select=".myContent"></content>
</p-bar>
</template>
<script>
Polymer( {
is: 'p-foo',
} )
</script>
</dom-module>

bar.html:

<dom-module id="p-bar">
<template>
bar open
<content select=".myContent"></content>
bar closed
</template>
<script>
Polymer( {
is: 'p-bar',
} )
</script>
</dom-module>

演示.html:

    <head>    
...
<link rel="import" href="foo.html">
</head>
<body>
<p-foo><div class="myContent"><strong>hello</strong></div></p-foo>
</body>
</html>

关于javascript - 嵌套 polymer 组件内容问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39460592/

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