我是 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>
在 index.html(主页)上,我使用
<self-login
sign-in-success="onSignedIn"
></self-login>
问题:在 onSignedIn() 回调中,我会将我的页面路由到/content。我能怎么做?
编辑 1:正如@zacharytamas 所建议的,我尝试使用 app-router 如下
索引.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>
主页.html
<dom-module id="home-page" noscript>
<template>
<h2>Hello</h2>
</template>
</dom-module>
当我浏览到 http://localhost:3000/ 时,它显示一个空白页面和 http://localhost:3000/test在 Chrome 上。任何的想法?
最佳答案
默认情况下,Polymer 没有内置路由。但是,有几个前端路由框架可以与 Polymer 一起使用。
一个非常常用的方法是一个名为 app-router 的自定义元素。 ,它允许您仅使用 HTML 以声明方式定义路由。我之前已经取得了一些成功。退房 the website有关设置的信息。
另一种基于 HTML 的方法是由 Polymer 团队成员制作的自定义元素,名为 more-routing . Google 有一个关于 Polymer 的视频系列,名为 Polycasts其中made a video explaining the more-routing
approach .您应该查看该视频以了解有关入门的信息。
另一种选择是使用 page.js framework 使用 JavaScript 来实现。 .这是Polymer Starter Kit使用的方法. Here's another Polycast让你开始这样做。
欢迎来到 polymer 世界!
关于Polymer 1.0 - 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30670425/