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访问。
答案 0 :(得分:3)
在元素内部this.username
但您可能需要将其添加到属性才能使其工作
Polymer({
is: 'my-element',
properties: {
username: {
type: Object,
observer: '_updateUsername'
}
},
_updateUsername: function () {
// use this.username
}
});
答案 1 :(得分:1)
<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