Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(typescript): add examples of TypeScript location and lifecycle interfaces #432

Merged
merged 1 commit into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions demo/vaadin-router-getting-started-demos.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,39 @@ <h3>Getting Started</h3>
<code>this.location.pathname</code>).
</p>

<h3>Using <code>this.location</code></h3>
<p>
If you use Polymer and JavaScript to build your view components,
the <code>location</code> property does not require a declaration.
It is recommended to add it to the <code>properties</code> of your
component for consistency, though:
</p>
<pre><code>static get properties() {
return {
// ...
location: Object
};
}</code></pre>
<p>
For LitElement and TypeScript a declaration in the component is required.
Declare the <code>location</code> property in the class and initialize it
from the <code>router</code> Vaadin Router instance:
</p>
<pre><code>import {router} from './index';
import {customElement, html, LitElement, property} from 'lit-element';

@customElement('my-view')
class MyViewElement extends LitElement {
@property({type: Object}) location = router.location;
Copy link
Contributor

@abdonrd abdonrd Feb 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not something like:

import { RouterLocation } from '@vaadin/router';

...

  @property({ type: Object })
  private location!: RouterLocation;

Copy link
Contributor

@abdonrd abdonrd Feb 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because, right now, I have no access to the router.

I'm using a init function like:
https://github.com/Polymer/pwa-starter-kit/compare/master...abdonrd:vaadin-router?expand=1#diff-15ee858ae5d5aa9b3e2846cfd1c4c466R3
(but in a TypeScript project)


render() {
return html`
Current location URL: ${this.location.getUrl()}
`;
}
}</code></pre>
<p>This property is automatically updated on navigation.</p>

<h3>Fallback Routes (404)</h3>
<p>If Vaadin Router does not find a matching route, the promise returned
from the <code>render()</code> method gets rejected, and any content in
Expand Down
84 changes: 84 additions & 0 deletions demo/vaadin-router-lifecycle-callbacks-demos.html
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,90 @@ <h3>Getting the Current Location</h3>
property is used later when processing the <code>vaadin-router-location-changed
</code> events.
</p>

<h3>TypeScript Interfaces</h3>
<p>
For using with components defined as TypeScript classes, the following
interfaces are available for implementing:
</p>
<ul>
<li>
<p><b><code>BeforeEnterObserver</code></b></p>
<pre><code>import {
BeforeEnterObserver,
PreventAndRedirectCommands,
Router,
RouterLocation
} from '@vaadin/router';

@customElement('my-view-with-before-enter')
class MyViewWithBeforeEnter extends LitElement implements BeforeEnterObserver {
onBeforeEnter(
location: RouterLocation,
commands: PreventAndRedirectCommands,
router: Router) {
// ...
}
}</code></pre>
</li>
<li>
<p><b><code>AfterEnterObserver</code></b></p>
<pre><code>import {
AfterEnterObserver,
EmptyCommands,
Router,
RouterLocation
} from '@vaadin/router';

@customElement('my-view-with-after-enter')
class MyViewWithAfterEnter extends LitElement implements AfterEnterObserver {
onAfterEnter(
location: RouterLocation,
commands: EmptyCommands,
router: Router) {
// ...
}
}</code></pre>
</li>
<li>
<p><b><code>BeforeLeaveObserver</code></b></p>
<pre><code>import {
BeforeLeaveObserver,
PreventCommands,
Router,
RouterLocation
} from '@vaadin/router';

@customElement('my-view-with-before-leave')
class MyViewWithBeforeLeave extends LitElement implements BeforeLeaveObserver {
onBeforeLeave(
location: RouterLocation,
commands: PreventCommands,
router: Router) {
// ...
}
}</code></pre>
</li>
<li>
<p><b><code>AfterLeaveObserver</code></b></p>
<pre><code>import {
AfterLeaveObserver,
EmptyCommands,
Router,
RouterLocation
} from '@vaadin/router';

@customElement('my-view-with-after-leave')
class MyViewWithAfterLeave extends LitElement implements AfterLeaveObserver {
onAfterLeave(
location: RouterLocation,
commands: EmptyCommands,
router: Router) {
// ...
}
}</code></pre>
</li>
</ul>
</template>
<script>
class VaadinRouterLifecycleCallbacksDemos extends DemoReadyEventEmitter(ElementDemo(Polymer.Element)) {
Expand Down