聚合物1.0 - 路由

Bao*_* Le 15 polymer

我是Polymer的新手.我开始编写一个简单的webapp,其中1.用户首先登陆登录页面2.如果用户通过登录,则将用户引导至内容页面

我写了一个元素"登录"

<dom-module id="login">
  <template>
    <!-- local DOM for your element -->
    <p>Username</p>
    <input class="paper-font-body2" value="{{email::change}}" type="email">
    <br/>
    <p>Password</p>
    <input class="paper-font-body2" value="{{password::change}}" type="password">
    <p>{{errorMessage}}</p>

    <iron-ajax
      id="ajax"
      url=""
      method="POST"
      on-response="signInResponse"
      debounce-duration="300">
    </iron-ajax>

    <button on-click="signIn">Signin</button>
  </template>
</dom-module>

<script>
  // element registration
  Polymer({
    is: "login",

    // add properties and methods on the element's prototype
    properties: {
      // declare properties for the element's public API
      email: {
        type: String,
        value: "username"
      },
      password: {
        type: String,
        value: "password"
      },
      errorMessage: {
        type: String,
        value: ""
      }
    },

    signIn: function() {
      this.$.ajax.url = "http://myserver/login/email";
      this.$.ajax.params = {"email":this.email, "password": this.password};
      this.$.ajax.generateRequest();
    },

    signInResponse: function(request) {
      response = request.detail.response;
      console.log(response);
      if (response.code == '0') {
        this.fire("signin-success", response);
      } else {
        this.fire("signin-fail", response);
      }
    }
  });
</script>
Run Code Online (Sandbox Code Playgroud)

在index.html(主页)上,我使用

<self-login
      sign-in-success="onSignedIn"
      ></self-login>
Run Code Online (Sandbox Code Playgroud)

问题:在onSignedIn()回调中,我将页面路由到/ content.我能怎么做?

编辑1:正如@zacharytamas建议的那样,我尝试使用app-router如下

的index.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
  <title>app-router</title>
  <script src="../bower_components/webcomponentsjs/webcomponents-lite.js"></script>
  <link rel="stylesheet" href="styles/main.css" shim-shadowdom>
  <link rel="import" href="../bower_components/app-router/app-router.html">
</head>
<body unresolved>
<app-router>
  <app-route path="/" import="/elements/home-page.html"></app-route>
  <app-route path="/test" import="/elements/home-page.html"></app-route>
  <app-route path="*" import="/elements/not-found-page.html"></app-route>
</app-router>

<script src="scripts/app.js"></script>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

家庭page.html中

<dom-module  id="home-page" noscript>
  <template>
    <h2>Hello</h2>
  </template>
</dom-module>
Run Code Online (Sandbox Code Playgroud)

当我浏览到Chrome上的http:// localhost:3000 /http:// localhost:3000/test时,它会显示一个空白页面.任何的想法?

小智 26

默认情况下,Polymer没有内置路由.但是,有几个前端路由框架可以与Polymer一起使用.

一种非常常用的方法是名为app-router的自定义元素,它允许您仅使用HTML来声明性地定义路由.我之前已经取得了一些成功.查看网站以获取有关设置的信息.

另一种基于HTML的方法是由Polymer团队成员称为更多路由的自定义元素.谷歌有一个关于聚合物的视频系列名为Polycasts,它制作了一个视频来解释这种more-routing方法.您应该查看该视频,了解有关入门的信息.

另一种选择是使用page.js框架使用JavaScript .这是Polymer Starter Kit使用的方法.这是另一个让你以这种方式开始的Polycast.

欢迎来到Polymer世界!


Eri*_*son 8

好消息!Polymer团队发布了官方路由器.他们的博客上有一个很好的介绍:

https://www.polymer-project.org/1.0/blog/routing

而Github回购的Readme.md非常有启发性:

https://github.com/PolymerElements/app-route

基本功能有两个必要元素,<app-location><app-route>

以下是一个简单的例子:

<app-location route="{{route}}"></app-location>
<app-route
    route="[[route]]"
    pattern="/users"
    active="{{usersRouteIsActive}}">
</app-route>
Run Code Online (Sandbox Code Playgroud)

在上面的例子中usersRouteIsActive,true如果路由匹配/users,将收到一个布尔值,如果不匹配,则收到false.简单吧?在此之后,随着应用程序的路由变得更加复杂,app-route元素具有更多支持这些需求的功能.