Vue 2 Internal Features from the Ground Up

Challenge 13: Solution

Evan You

Evan You

Creator of Vue.js
Vue 2 Internal Features from the Ground Up

Check out a free preview of the full Vue 2 Internal Features from the Ground Up course

The "Challenge 13: Solution" Lesson is part of the full, Vue 2 Internal Features from the Ground Up course featured in this preview video. Here's what you'd learn in this lesson:

Evan walks through the solution to Challenge 13.


Transcript from the "Challenge 13: Solution" Lesson

>> Evan You: So the very obvious, well, not really obvious but we need to keep the URL as a piece of reactive state inside our app, in order for our app to react to the changes. So our root and probably, should have data property called url to hold the current url.

And where do we get the url? We get it by retrieving window.location.hash, and we can slice off the leading hash, just so things look a bit cleaner. But the important thing is, you want to initialize your app URL with that hash, when the app boots. This is important for passing the test because, if you directly visit your page with hash foo.

And you don't initialize, you're always something like null or something, right? Then, no components will be matched, and your page will just be blank when people visit an existing, directly visit a hashed drl. Which is not good. So initializing your f state to width with the hash on start up, it's important to enable deep linking.

And then, inside the hashchange listener, we just update the URL. So this is enough to trigger reactive updates, causing you to update your app. And next is, in this case, I'm using two components here. But if you are lazy or you just want to get it working, you can in fact just do something like url.

[LAUGH] That's pretty much it. But in most cases, when you do routing, you are switching between pages, virtual pages, right? And in the context of view or most of today's frameworks, pages are just components, right? It just boxes all the way down. So we use components, top level components to represent the pages.

And really when you talk about switching pages in a single page application app, you're switched between components. So which is why the component is syntax is, well suited for this use case. Alternatively, you can get rid of all this and get renofunctions, remembering renofunctions, we were able to, we'll actually talk about that later.

Now, our components are standardly registered under fluent bar, and we use component is url to match that. This should give us the desired result in the browser. Really, all you need to do is make sure the URL is reactive and that there is a way to translate URL into a component that's rendered in your template

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now