Params

A path segment that starts with a colon is a parameter. /albums/:id matches /albums/a1, /albums/a7 and every other album, and the matched value reaches the view as params.id.

That is what the first argument of data(params, props) is for. The router runs data() before the page renders, so the view can look the album up and have it ready. Music has no datastore yet, so read the ALBUMS array. Chapter 4 swaps it for the store.

The shop's cards already link to /albums/ plus each album's id.

Your turn

  1. In routes.js, import AlbumView from ./views/Album.pzl and add a route for /albums/:id.
  2. In Album.pzl, return the album whose id matches params.id:
data(params, props) {
  return { album: ALBUMS.find((a) => a.id === params.id) };
}
  1. Click a cover in the preview.

Parameter routes are fine in an app like this one. Only a statically built site, the kind from lesson 2.4, has to list every path up front.

Chapters