[英]Change Polymer 3 route to a different html page
我正在創建一個帶有登錄和儀表板頁面的示例Polymer 3應用程序。 我的后端是一個簡單的JX-RS REST API。 從Web服務獲得響應后,我想轉到一個新的HTML文件(讓我們說dashboard.html
),而不是路由到同一頁面中的另一個元素。 當我瀏覽官方網站時 ,因為我是JS本身的初學者,所以我無法真正理解該如何進行。
在我的index.html
,我有<my-login>
,在響應之后,將調用handlePositiveResponse
。 這是我更改路線的地方。 請在下面找到方法。
以下是我的代碼: login.js
class MyLogin extends PolymerElement {
static get template() {
return html`
<h1>Hello World..!!</h1>
<iron-form id="loginUserForm" on-iron-form-response="handlePositiveResponse" on-iron-form-error="handleNegativeResponse">
<form id="innerLoginUserForm" content-type="application/json" method="post">
<paper-input id="email" name="email" label="E-Mail Address"></paper-input>
<paper-input id="password" name="password" label="Password" type="password"></paper-input>
<paper-button disabled="{{activeRequest}}" raised id="loginButton" on-tap="submitLogin">Sign In</paper-button>
</form>
</iron-form>
`;
}
static get properties() {
return {
page: {
type: String,
reflectToAttribute: true,
observer: '_pageChanged'
},
routeData: Object,
subroute: Object
};
}
static get observers() {
return [
'_routePageChanged(routeData.page)'
];
}
_routePageChanged(page) {
// Show the corresponding page according to the route.
//
// If no page was found in the route data, page will be an empty string.
// Show 'view1' in that case. And if the page doesn't exist, show 'view404'.
if (!page) {
this.page = 'login';
} else if (['view1', 'view2', 'view3', 'my-dashboard'].indexOf(page) !== -1) {
this.page = page;
} else {
this.page = 'view404';
}
// Close a non-persistent drawer when the page & route are changed.
// if (!this.$.drawer.persistent) {
// this.$.drawer.close();
// }
}
_pageChanged(page) {
// Import the page component on demand.
//
// Note: `polymer build` doesn't like string concatenation in the import
// statement, so break it up.
switch (page) {
case 'view1':
import('./my-view1.js');
break;
case 'view2':
import('./my-view2.js');
break;
case 'view3':
import('./my-view3.js');
break;
case 'my-dashboard':
import('./my-dashboard.js');
break;
case 'view404':
import('./my-view404.js');
break;
}
}
submitLogin(e) {
var form = this.shadowRoot.querySelector('#loginUserForm');
this.shadowRoot.querySelector('#innerLoginUserForm').action = 'http://localhost:8080/PolymerJavaBackend/rest/login'
form.submit();
}
handlePositiveResponse(response) {
this._routePageChanged(null);
this._routePageChanged('my-dashboard');
console.log(response);
}
handleNegativeResponse(error) {
console.log(error);
}
如果您可以提出建議,將不勝感激如何路由到新的html頁面。
那是您的頂級元素嗎? 在頂層元素中,應該在其中具有Iron-pages元素,其中包含應用程序中的所有頁面,還必須在其中添加新頁面。
在那里,您應該具有_routePageChanged來檢查路線數據更改。 和_pageChanged導入頁面。
如果要導航到新頁面(而不是從菜單中),而是要從服務器獲取響應時,請在handlePositiveResponse(response)中使用雙向數據綁定或this.set來更新路由值: this.set('route.path', '/my-dashboard');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.