|
| 1 | +# Adding a Route |
| 2 | + |
| 3 | +Alright, now let's add another route to our app. We'll call it 'foo'. We can easily do this with the `yo angular-fullstack:route` subgenerator command: |
| 4 | + |
| 5 | +```bash |
| 6 | +$ yo angular-fullstack:route foo |
| 7 | +? What module name would you like to use? (aftestApp.foo) |
| 8 | +? What module name would you like to use? aftestApp.foo |
| 9 | +? Where would you like to create this route? (client/app/) |
| 10 | +? Where would you like to create this route? client/app/ |
| 11 | +? What will the url of your route be? (/foo) |
| 12 | +? What will the url of your route be? /foo |
| 13 | +identical client\app\foo\foo.routes.js |
| 14 | +identical client\app\foo\foo.component.js |
| 15 | +identical client\app\foo\foo.component.spec.js |
| 16 | +identical client\app\foo\foo.html |
| 17 | +identical client\app\foo\foo.scss |
| 18 | + |
| 19 | +In the parent of this component, you should now import this component and add it as a dependency: |
| 20 | + |
| 21 | + import FooComponent from './foo/foo.component'; |
| 22 | + ... |
| 23 | + export angular.module('myParentModule', [FooComponent]); |
| 24 | +``` |
| 25 | + |
| 26 | +We give it our route name ('foo'), and a few more details: the name of the Angular module to create ('myApp.foo'), which folder to put the route under ('client/app/foo/'), and the URL of the route ('localhost:3000/foo'). |
| 27 | + |
| 28 | +This will create an Angular 1.5 component with an Angular module (`foo.component.js`), a template file (`foo.html`), a CSS file (`foo.scss`), a unit test file (`foo.component.spec.js`), and a routing file (`foo.routes.js`). |
| 29 | + |
| 30 | +Since we're using Webpack, We'll need to import our component somewhere. Since this is a generic app route (and for simplicity), we'll import it in `app.js`, under our root Angular module, like so: |
| 31 | + |
| 32 | +`client/app/app.js` |
| 33 | +```js |
| 34 | +... |
| 35 | +import FooModule from './foo/foo.component'; |
| 36 | +angular.module('aftestApp', [ |
| 37 | + ... |
| 38 | + main, |
| 39 | + FooModule, |
| 40 | +]) |
| 41 | + .config(routeConfig) |
| 42 | + .run(...); |
| 43 | + |
| 44 | +angular.element(document) |
| 45 | + .ready(() => { |
| 46 | + angular.bootstrap(document, ['aftestApp'], { |
| 47 | + strictDi: true |
| 48 | + }); |
| 49 | + }); |
| 50 | +``` |
| 51 | + |
| 52 | +Now that we've imported our new Angular module and added it to the dependency list of our root Angular module, we should be able to navigate to `http://localhost:3000/foo` and see our new route: |
| 53 | + |
| 54 | +<img src="../images/foo-route.jpg" alt="Foo route screenshot"> |
| 55 | + |
| 56 | +It's not a very impressive page right now, but it works. |
| 57 | + |
| 58 | +Now, our user's aren't going to know to go to the `/foo` route. Let's add a navbar entry for it. |
| 59 | + |
| 60 | +`client/components/navbar/navbar.component.js` |
| 61 | +```js |
| 62 | +import angular from 'angular'; |
| 63 | + |
| 64 | +export class NavbarComponent { |
| 65 | + menu = [{ |
| 66 | + title: 'Home', |
| 67 | + state: 'main' |
| 68 | + }, { |
| 69 | + title: 'Foo', |
| 70 | + state: 'foo' |
| 71 | + }]; |
| 72 | + isCollapsed = true; |
| 73 | + |
| 74 | + constructor(Auth) { |
| 75 | + 'ngInject'; |
| 76 | + this.isLoggedIn = Auth.isLoggedInSync; |
| 77 | + this.isAdmin = Auth.isAdminSync; |
| 78 | + this.getCurrentUser = Auth.getCurrentUserSync; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +export default angular.module('directives.navbar', []) |
| 83 | + .component('navbar', { |
| 84 | + template: require('./navbar.html'), |
| 85 | + controller: NavbarComponent |
| 86 | + }) |
| 87 | + .name; |
| 88 | +``` |
| 89 | + |
| 90 | +Easy enough. Now we should see our entry for 'Foo' in our navbar. It should also be highlighted if you're still on the '/foo' route. |
| 91 | + |
| 92 | +<img src="../images/foo-route-navbar.jpg" alt="Foo route screenshot"> |
| 93 | + |
| 94 | +You can read about all the other subgenerators that are available in the [Generators](../Generators) section of the docs. |
0 commit comments