简体   繁体   English

聚合物,如何将page.js中的url参数转换为聚合物元素?

[英]Polymer, how to get url params in page.js into polymer element?

I want to make a single page application. 我想制作单页应用程序。 When I go to path like localhost/person-info/101 , how can I get the URL params using page.js into a Polymer element? 当我去localhost/person-info/101类的路径时,如何使用page.js将URL参数转换为Polymer元素?

I have to use these params to select or change data in the person-info element before using neon-animated-page to change page 在使用neon-animated-page更改页面之前,我必须使用这些参数来选择或更改person-info元素中的数据

Contents of app.js : app.js内容:

window.addEventListener('WebComponentsReady', function () {
    var app = document.querySelector('#app');
    page('/', home);
    page('/person-info/:user', showPersonInfo);

    page({
        hashbang: true
    });

    function home() {
        app.route = 'index';

        console.log("app route" + app.route);
        console.log("home");
    }

    function showPersonInfo(data) {
        console.log(data);
        console.log(data.params.user);

        app.params = data.params;

        app.route = 'person-info';
    }
});

And my Polymer element person-info.html 还有我的Polymer元素person-info.html

<link rel="import" href="bower_components/iron-list/iron-list.html">
<link rel="import" href="bower_components/polymer/polymer.html">
<link rel="import" href="bower_components/paper-toolbar/paper-toolbar.html">
<link rel="import" href="bower_components/paper-icon-button/paper-icon-button.html">

<script type="text/javascript" src="bower_components/jquery-2.1.4.min/index.js"></script>
<script src="js/app.js"></script>

<dom-module id="person-info">
    <style>
        :host {
            display: block;
            font-family: sans-serif;
            @apply(--layout-fit);
            @apply(--layout-vertical);
        }

        .toolbar {
            background: #3f78e3;   
        }

        a {
            color:#FFF;
        }
    </style>
    <template id="person-info">
        <paper-toolbar class="toolbar">
            <a href="/">
                <paper-icon-button icon="icons:arrow-back"></paper-icon-button>
            </a>

            <div>test</div>
            <div>test</div>
        </paper-toolbar>
        <content></content>
    </template>
    <script>
        Polymer({
            is: 'person-info',
            ready: function () {
                console.log("person-info page");
                this.testdd = "adisak";
                console.log("user ---->"+this.params);
                console.log(this);
            },
            properties: {
                dataindex: String
            }
        });
    </script>
</dom-module>

Variable app is global, so if you bind a data to app.params from your route than you can access it everywhere using app.params. 变量app是全局的,因此如果您将数据绑定到app.params ,那么您可以使用app.params在任何地方访问它。 Or just access your element and set the properties on your element like below 或者只是访问您的元素并在元素上设置属性,如下所示

function showPersonInfo(data){
    var personInfo=document.querySelector('person-info');
    personInfo.user=data.params;
    app.route = 'person-info';
}

From your person-info element you can access by using this.user but i think you need to set the property user on element properties. 从您的person-info元素可以使用this.user访问,但我认为您需要在元素属性上设置属性user

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM