首页 > 解决方案 > Is it possible to make a parent route a 404, but the child route a specific route

问题描述

In my router.js file, I have something that looks like the following

import EmberRouter from '@ember/routing/router';

const Router = EmberRouter.extend({});

Router.map(function () {
  this.route('index', { path: '/' });
  this.route('foo', function() {
    this.route('bar', function() {
      this.route('cat');
    });
  });

  this.route('notfound', { path: '/*path' });
});

export default Router;

The problem is that I want the path /foo/bar/cat to route to the foo.bar.cat route, but I don't want the paths /foo or /foo/bar to route to anything except a 404. In my case, the foo and bar routes are essentially useless. I just want the url to have 3 levels for aesthetic purposes.

标签: ember.jsember-router

解决方案


我实际上意识到更有意义的是避免创建foobar路由并为我想要的路由创建路径。

所以不要在 ember-cli 中这样做:

ember g route foo/bar/cat

我已经做了

ember g route cat

然后我在路由器中设置所需的路径。

Router.map(function () {
  this.route('index', { path: '/' });
  this.route('cat', { path: '/foo/bar/cat'});
  this.route('notfound', { path: '/*path' });
});

这为我提供了正确的路线foo/bar/cat,但foo两者foo/bar都被路由到该notfound路线。


推荐阅读