本文介绍了Ember和Express:让Ember处理路由而不是Express?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但是我正在Express服务器上使用 ember-cli 提供的Ember应用程序,但是当我尝试访问各种路由,我的Express应用程序错误,说没有路由存在(这是真的,因为我定义了Ember中的路由,而不是Express)。我应该如何解决这个问题,这是正常的行为吗?

This might be a dumb question, but I'm serving an Ember app I made using ember-cli on an Express server, but when I try to access various routes, my Express app errors, saying that no route exists (which is true, because I defined the routes in Ember, not Express). How should I resolve this, and is this normal behavior?

我的Ember路由器:

My Ember router:

Router.map(function() {
  this.route('index', {path: '/' });
  this.route('portkey');
  this.route('login');
});

我的快速路线只是一个不提供任何Ember路由的API,因为 localhost:1234 将自动加载 index.html

My Express routes are just an API that do not serve any of the Ember routes, since localhost:1234 will automatically load index.html.

推荐答案

我从来没有使用Ember Router而不是Express路由器出现问题。我所做的只有一条快速路线(用于'/'),显示我的Ember应用程序index.html(实际上是index.ejs)页面。不承诺这是 正确的方法,但这是我做的,它适用于我。

I've never had a problem using the Ember Router instead of the Express router. All I do is have 1 express route (for '/') which displays my Ember application index.html (well actually index.ejs) page. Not promising this is the right way to do it, but it's how I do it and it works for me.

所以从这开始。

app.get('/', function(req, res) {
    res.render('index', {});
});

这是你的快递路线。现在你的ember路由。

That's your express route. Now your ember routing.

App.Router.map( function() {
    this.route("about", { path: "/about" });
    this.route("favorites", { path: "/favorites" });
});

所以现在你有一个如下所示的路由结构:

So as of now you have a routing structure that looks like the following:

yourdomain.com/             --> index.ejs displayed via express routing
              /#/           --> this is the ember index route
              /#/about      --> this is the ember about route
              /#/favorites  --> this is the ember favorites route

在index.ejs文件中,您将基本的ember文件链接到ember应用程序。

Within the index.ejs file you have the basic ember file linking to your ember application.

现在到您的链接问题...

Now onto your linking problems...

如果您使用ember路由器,确定您正确的方式链接到您的不同路线。 (记住,ember路线从/#/ someroute开始)。

If you use the ember router, then make sure you are linking to your different routes the correct way. (Remember, ember routes start with /#/someroute).

所以你的手柄链接应该是这样的:

So your links in handlebars should be something like:

{{#link-to 'some_page'}}Go to some page{{/link}}

<a href="some_page">Go to some page</a>

使用第二个,express将尝试处理路由,但是通过使用第一个,ember正在处理路由。

Using the second, express would be trying to handle the routing but by using the first, ember is handling the routing.

所以如果你真的想到,你可以拥有尽可能多的ember应用程序,因为你的小心脏因为每个ember应用程序链接到当前页面快递路由。

So if you really think about it, you can have as many ember applications as your little heart disires because each ember application is linked to that current page in the express routing.

例如在我的网站上,我使用两条路由(再加上一堆REST路由): login.ejs index.ejs

For example on my website, I use two routes (plus a bunch of REST routes obviously): login.ejs and index.ejs.

所以对于,我有以下路线:

So for my site, I have the following routes:

mysite.com/
          /#/
          /#/budget
          /#/history
          /#/profile
          /#/logout

mysite.com/login#/
                #/register
                #/forget

我希望这有助于您位。

I hope this helps you a little bit.

编辑

/#/ 是一个惯例,告诉您通过路由器进行路由。

/#/ is a convention to tell ember you are routing via its router.

想像这样:Ember是一个单页框架。所以当你从页面到页面链接时,你不会真正地更改页面。你只是删除dom元素并用新的元素替换它们。但是,如果您在服务器上进入 / budget ,那么现在您将需要一个全新的页面,而不仅仅是 /#/ budget ember应用程序部分。

Think of it like this: Ember is a single-page framework. So when you link from page to page in ember, you aren't truely changing pages. You are just removing dom elements and replacing them with new ones. But if you go to /budget on the server, you are now going to a whole new page, not just the /#/budget section of the ember application.

我认为你只是混淆了ember路由器的真正原因。

I think you are just confusing what the ember router really is.

这篇关于Ember和Express:让Ember处理路由而不是Express?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:09