Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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
Templates 聚合物:基于属性的同一组件的多个模板?_Templates_Syntax_Polymer_Logic - Fatal编程技术网

Templates 聚合物:基于属性的同一组件的多个模板?

Templates 聚合物:基于属性的同一组件的多个模板?,templates,syntax,polymer,logic,Templates,Syntax,Polymer,Logic,我在这里略显灰白,试图弄清楚如何在一个聚合物组分中使用多个模板,并根据属性解析为一个特定的模板 以下是场景: 组件 <dom-module id="py-test"> <template> Foo </template> <template> Bar </template> <script> Polymer({ i

我在这里略显灰白,试图弄清楚如何在一个聚合物组分中使用多个模板,并根据属性解析为一个特定的模板

以下是场景:

组件

<dom-module id="py-test">
    <template>
        Foo
    </template>
    <template>
        Bar
    </template>
    <script>
        Polymer({
            is: "py-test"
        })
    </script>
</dom-module>

福
酒吧
聚合物({
是:“py测试”
})
标记

<py-test bar></py-test>

预期结果 如果
具有bar属性,则组件应使用第二个模板。否则,它应该使用第一个模板

有没有人知道如何让聚合物屈服

//编辑

<dom-module id="py-test">
    <template>
        Foo
    </template>
    <template>
        Bar
    </template>
    <script>
        Polymer({
            is: "py-test"
        })
    </script>
</dom-module>
Maria指出dom if是一个可能的解决方案。这是一种工作。我使用上面的示例添加dom if检查:

<dom-module id="py-test">
    <template>
        <template is="dom-if" if="[[foo]]">
            Foo
        </template>
        <template is="dom-if" if="[[bar]]">
            Bar
        </template>
    </template>
    <script>
        Polymer({
            is: "py-test",
            properties: {
                foo: {
                    type: Boolean,
                    value: true
                },
                bar: {
                    type: Boolean,
                    value: false
                }
            }
        })
    </script>
</dom-module>

福
酒吧
聚合物({
是:“py测试”,
特性:{
傅:{
类型:布尔型,
值:true
},
酒吧:{
类型:布尔型,
值:false
}
}
})

但我不知道如何将其与
元素本身上设置的属性联系起来。

您的编辑就快完成了。只需删除
属性
对象中的初始值

<dom-module id="py-test">
    <template>
        <template is="dom-if" if="[[foo]]">
            Foo
        </template>
        <template is="dom-if" if="[[bar]]">
            Bar
        </template>
    </template>
    <script>
        Polymer({
            is: "py-test",
            properties: {
                foo: {
                    type: Boolean,
                },
                bar: {
                    type: Boolean,
                }
            }
        })
    </script>
</dom-module>

福
酒吧
聚合物({
是:“py测试”,
特性:{
傅:{
类型:布尔型,
},
酒吧:{
类型:布尔型,
}
}
})
然后您可以这样使用它:

<py-test foo></py-test>
<py-test bar></py-test>

这是一个例子


Polymer处理布尔属性的方式可能会有点混乱。如果它们存在,则关联的属性为
true
,否则为
false
。如果你设置了初始值,你会干扰它。

如果使用
dom,你会选择它吗?@Maria如果我可以通过检查特定函数的真实性在模板之间切换,那么它在某种程度上可以与dom一起工作。但这对我帮助不大,因为我不知道如何将这些布尔结果与markup元素上设置的特定属性相关联。有什么想法吗?也许这就是你要找的。使用Polymer.Templatizer行为根据属性标记模板必须忽略
属性中的值,然后它才能工作。看到这个,我将把它作为答案发布…非常好的解释。事实上,聚合物有它的怪癖,但大多数框架都有。非常感谢。