如何访问聚合物中的属性值?
How to access attribute value in polymer?
HTML
<my-element username="{{params.username}}"></my-element>
元素
<dom-module id="my-element">
<style>
:host {
display: block;
}
</style>
<template>
</template>
<script>
(function() {
Polymer({
is: 'my-elemnt',
ready: function(){
// get attribute value
});
})();
</script>
</dom-module>
我可以从模板访问属性,但不知道如何从 javascript 访问。
在元素内部它将是 this.username
但您可能需要将其添加到属性中才能正常工作
Polymer({
is: 'my-element',
properties: {
username: {
type: Object,
observer: '_updateUsername'
}
},
_updateUsername: function () {
// use this.username
}
});
<link rel="import" href="polymer/polymer.html"/>
<dom-module id="my-element" username>
<style>
:host {
display: block;
}
</style>
<template>
<p>{{username.name}}</p>
</template>
</dom-module>
<script>
(function () {
Polymer({
is: 'my-elemnt',
properties: {
username: {
type: Object,
notify: true
}
},
ready: function () {
console.log(this.username);
}
});
})();
</script>
如果您还没有声明 属性 它将记录 - undefined
有关新申报方式的更多信息,请查看 -
https://www.polymer-project.org/1.0/docs/devguide/properties.html
HTML
<my-element username="{{params.username}}"></my-element>
元素
<dom-module id="my-element">
<style>
:host {
display: block;
}
</style>
<template>
</template>
<script>
(function() {
Polymer({
is: 'my-elemnt',
ready: function(){
// get attribute value
});
})();
</script>
</dom-module>
我可以从模板访问属性,但不知道如何从 javascript 访问。
在元素内部它将是 this.username
但您可能需要将其添加到属性中才能正常工作
Polymer({
is: 'my-element',
properties: {
username: {
type: Object,
observer: '_updateUsername'
}
},
_updateUsername: function () {
// use this.username
}
});
<link rel="import" href="polymer/polymer.html"/>
<dom-module id="my-element" username>
<style>
:host {
display: block;
}
</style>
<template>
<p>{{username.name}}</p>
</template>
</dom-module>
<script>
(function () {
Polymer({
is: 'my-elemnt',
properties: {
username: {
type: Object,
notify: true
}
},
ready: function () {
console.log(this.username);
}
});
})();
</script>
如果您还没有声明 属性 它将记录 - undefined
有关新申报方式的更多信息,请查看 -
https://www.polymer-project.org/1.0/docs/devguide/properties.html