If you think that html5 has changed the web, then wait and see what the Web components will do. © someone's.
platform.js
) and everything will continue to work natively (UPD: made a clarification. Thanks to nazarpc ) . We understand that companies like Google and Mozilla (they are developing a similar x-tag) can change the future of the web. npm install -g bower npm install -g yo npm install -g generator-polymer mkdir my-project cd my-project yo polymer
grunt serve
index.html
must be connected (we already have everything connected) platform.js
: <script src="bower_components/platform/platform.js"></script>
<link>
tag the elements we need before using them: <link rel="import" href="bower_components/[]/[].html">
<core-scaffold> <core-header-panel mode="seamed" navigation flex> <core-toolbar></core-toolbar> <core-menu valueattr="label"> <core-item icon="settings" label="Item1"></core-item> <core-item icon="settings" label="Item2"></core-item> </core-menu> </core-header-panel> <google-map></google-map> </core-scaffold>
<google-map></google-map>
. This makes it easy to embed Google services in our application. See the list of components on googlewebcomponents.imtqy.com .<habrauser-card>
element. I'll start with an empty habrauser-card.html
in the app/elements
folder): <link rel="import" href="../bower_components/polymer/polymer.html"> <polymer-element name="habrauser-card" attributes="habrauser usercolor"> <template> <style> :host { display: block; padding: 10px; color: {{usercolor}}; } polyfill-next-selector { content: ':host h3'; } :host ::content h3 { margin: 0; text-decoration: underline; } </style> <content select="h3"></content> <li>I am habrauser <b>{{habrauser}}</b></li> <li><content></content></li> </template> <script> Polymer('habrauser-card', { habrauser: 'DefaultValue', usercolor: 'green' }); </script> </polymer-element>
polymer.html
or any other component, inside of which there is already a link on polymer.html
.<polymer-element>
, we define our element <habrauser-card>
(Important! The name must necessarily be with a '-' sign) . In the attributes
property we list the external (which will be used when calling) properties. I would call it a web component interface. They can still be set via js and the publish:
property, but the method of setting via the argument is preferable.<template>
tags).<style>
, or you can load an external file (just like in the usual html). Moreover, Polymer will delay the "registration" of the element until the style sheet is fully loaded. :host
is the selector of our <habrauser-card>
element, i.e. root.<content>
substitute what is enclosed in our <habrauser-card>
when called. The contents of the <habrauser-card>
can even be disassembled by 'internal' tags, and output in the right places. This is done using the select
argument of the <content>
.<script>
, in which js is natural, can also be loaded as a file. Polymer([tag-name,] [prototype])
is a convenient wrapper for binding properties and methods to an element.{{param}}
in the text, in the attributes, in the styles, everywhere and everything works. If the variable changes, it will change everywhere and immediately. Moreover, if this is an external variable (interface), then the changed value can be transferred "to the top".index.html
: <!-- --> <link rel="import" href="elements/habrauser-card.html"> ... <div class="hero-unit"> ... <!-- --> <habrauser-card habrauser='Petya' usercolor='red'>This text from index.html</habrauser-card> <habrauser-card habrauser='Ivan'><h3>Hello!</h3> The end text!</habrauser-card> <habrauser-card usercolor='blue'><input placeholder="type here"></habrauser-card> </div>
... <li>I am habrauser <b on-click="{{resetColor}}">{{habrauser}}</b></li> ... Polymer('habrauser-card', { habrauser: 'DefaultValue', usercolor: 'green', resetColor: function() { this.usercolor = 'green'; } ...
index.html
will also slightly correct it by wrapping our calls in the <template>
tag (for the work of date-binding in our index.html
). <template is="auto-binding"> <habrauser-card habrauser="Petya" usercolor="red">This text from index.html</habrauser-card> <habrauser-card habrauser="Ivan"><h3>Hello!</h3> The end text!</habrauser-card> <habrauser-card usercolor="{{newcolor}}"><input placeholder="type here" value="{{newcolor}}"></habrauser-card> </template>
repeat
it. Replace in index.html
: <div class="hero-unit"> <yo-greeting></yo-greeting> <p>You now have</p> <yo-list></yo-list> <template id="my-tpl" is="auto-binding" repeat="{{habrausers}}"> <habrauser-card habrauser="{{name}}" usercolor="{{color}}">{{text}}</habrauser-card> </template> </div> <script> document.querySelector('#my-tpl').habrausers = [ {name: 'Vasya', color: 'red', text: 'It is a good day!'}, {name: 'Petya', color: 'green', text: 'Are you clever?'}, {name: 'Masha', color: 'violet', text: 'I am beautifull!'}, {name: 'Ira', color: 'orange', text: 'It is a future!'} ]; </script>
Source: https://habr.com/ru/post/237421/
All Articles