简体   繁体   English

自定义函数中的聚合物未定义属性

[英]Polymer undefined property in custom function

I use Polymer (v1.0) and I want to create a simple element which can show me some extra content depending on a property and it has to work with multiple values - aka simple " switch ". 我使用Polymer (v1.0),并且我想创建一个简单的元素,该元素可以根据属性显示一些额外的内容并且它必须使用多个值 -也就是简单的“ switch ”。

I wrote this code. 我写了这段代码。

...
    <span>{{for}}</span>
    <template is="dom-if" if="{{checkFor('alfa')}}">
        hi !!!
    </template>
</template>

<script>
    Polymer({
        is: "...",

        properties: {
            for: String,
            user: String,
            manager: {
                type: Boolean,
                notify: true
            }
        },

        attached: function() {
            this.textContent = 'Hello World, my user is ' + (this.user || 'nobody') + '.\n' +
                    'This user is ' + (this.manager ? '' : 'not') + ' a manager and he likes ' + this.for + '.';
        },


        checkFor: function (aaa) {
            // return aaa === this.for;

            this.textContent = 'Hello World, my user is ' + (this.user || 'nobody') + '.\n' +
                    'This user is ' + (this.manager ? '' : 'not') + ' a manager and he likes ' + this.for + '.';
        }
    });
</script>

My function checkFor is used to check if a value I want to have (for example 'alfa') is equal with element property/attribute for . 我的功能checkFor用来检查一个值我想要(例如“阿尔法”)是与元件特性/属性等于for

But it doesn't worked at all so I tried to check values using console.log(). 但这根本不起作用,因此我尝试使用console.log()检查值。 After than I discovered variable aaa contains 'alfa' and variable this.for contains undefined . 之后,我发现变量aaa包含'alfa',变量this.for包含undefined

Then I copied some output code I found which use element properties and it also did not work. 然后,我复制了一些发现的输出代码,这些代码使用了元素属性,并且也不起作用。 If I run the same code in attached function it works fine. 如果我在attached函数中运行相同的代码,则可以正常工作。

How can I use element properties in custom functions? 如何在自定义函数中使用元素属性?

The first time your checkFor function is called, this.for has not been defined yet. 第一次调用checkFor函数时,尚未定义this.for You can pass in for as a parameter to the function, as described in the documentation . 您可以按照文档中的说明for作为参数传递给函数。

<template is="dom-if" if="{{checkFor('alfa', for)}}">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法读取Polymer中未定义的属性“ toLowerCase” - Cannot read property 'toLowerCase' of undefined in Polymer 自定义类的未定义属性 - Undefined property for custom class 自定义js函数'无法读取属性'缩放'未定义' - Custom js function 'Cannot read property 'zoom' of undefined ' 无法在React容器中的自定义onTouch函数中读取未定义的属性&#39;props&#39; - Cannot read property 'props' of undefined in custom onTouch function in React container Node.js JsonParser 自定义函数“无法读取未定义的属性” - Node.js JsonParser custom function "cannot read property of undefined" 在元素上设置属性会给出错误“无法通过Polymer设置属性&#39;...&#39;的undefined” - Setting attribute on element gives error "Cannot set property '…' of undefined' with Polymer 在forEach内部聚合物属性观察器中,数组变为“未定义” - Array becomes 'undefined' in forEach inside polymer property observer Polymer:未捕获的TypeError:无法读取未定义的属性“ persistent” - Polymer: Uncaught TypeError: Cannot read property 'persistent' of undefined 为什么这个聚合物元素的属性解析为未定义? - Why does this polymer element's property resolve to undefined? 应用程序路由到自定义元素的聚合物传递数据未定义 - Polymer pass data of app-route to custom element gone undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM