Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aurelia routing configuration issue

I am having an issue with configuring Aurelia route when the path like layer1/layer2 not just layer1. Here is the project file structure (files under dist gets created automatically based on files under src folder)

dist
    | - home
        | - home.html
        | - home.js
    | - user
        | - register.html
        | - register.js
    app.html
    app.js 
    main.js 
src
    | - home
        | - home.html
        | - home.js
    | - user
        | - register.html
        | - register.js
    app.html
    app.js 
    main.js 

When I do the following it just works fine:

app.html

<template>
  <div>
    <a href="user">register user</a>
    <a href="otherlink">otherlink</a>
  </div>
  <div class='main'>
    <router-view></router-view>
  </div>
</template>

app.js

this.router.configure(config => {
  config.title = 'demo';
  config.options.pushState = true;
  config.map([
    // home routes
    { route: ['','home'], moduleId: './home/home', nav: true, title:'Home' },

    // User register
    { route: ['user'], moduleId: './user/register', nav: true, title:'Register User'}
  ]);
});

But when I change the path from user to user/register like below, it no longer works

app.html

<template>
  <div>
    <a href="user/register">register user</a>
    <a href="otherlink">otherlink</a>
  </div>
  <div class='main'>
    <router-view></router-view>
  </div>
</template>

app.js

this.router.configure(config => {
  config.title = 'demo';
  config.options.pushState = true;
  config.map([
    // home routes
    { route: ['','home'], moduleId: './home/home', nav: true, title:'Home' },

    // User register
    { route: ['user/register'], moduleId: './user/register', nav: true, title:'Register User'}
  ]);
});

And in chrome debugger, I see this error:

GET http://localhost:9000/user/dist/user/register.html 404 (Not Found)

Notice that somehow an extra user gets added to the url which causes not to be able find register.html file. Again, when I am just using user as route, it works fine without any error but when I just change from user to user/register, it doesn't work anymore.

Could someone please let me know why this is happening and how to fix it?

like image 431
Golden mole Avatar asked Mar 24 '15 23:03

Golden mole


People also ask

How do I configure the router in Aurelia?

Basic Configuration To use Aurelia's router, your component view must have a <router-view></router-view>element. In order to configure the router, the component's view-model requires a configureRouter()function. <template><ulrepeat.for="nav of router.navigation"><liclass="${nav.isActive ?

How to enable dynamic/ runtime routing in Aurelia?

In the Aurelia Direct Router, dynamic/runtime routing is/will be possible primarily in two ways: by using direct routing (since it's doesn't have configured routes but instead resolves the routes during runtime), and

What is a layout in Aurelia?

The set of views subject to being part of a layout is defined in Aurelia as a set of views referenced by one or more routes in a router configuration. There are two ways to associate a layout with routes. The first is via HTML, the second is via view model code. We're going to be a little sloppy here in terminology.

How to redirect routes to URL fragments in Aurelia?

Aurelia allows redirecting of routes to URL fragments by specifying redirect with a string consisting of a URL fragment. config.map([{route:'',redirect:'home'},{route:'home',name:'home',moduleId:'home/index'}]);


2 Answers

Change the href for the link to be href="#/user/register" and that should clear up the issue for you.

I didn't see that you had pushState enabled. Please edit your index.html and add

<base href="http://localhost:9000/">

Or whatever your root url is. This should fix the issue for you.

like image 187
Ashley Grant Avatar answered Oct 17 '22 19:10

Ashley Grant


I made a small change to Ashley's answer which makes little bit more environmentally friendly since we don't need to hard code local url. I tested it and works well too.

<base href="/">
like image 7
Golden mole Avatar answered Oct 17 '22 18:10

Golden mole