Ember.js 在 App.Route 中指定位置=“历史记录”的 URL 类型时出错

Ember.js Error in specifying URL type of location='history' in App.Route

本文关键字:历史记录 记录 历史 URL 出错 类型 App js Route 位置 定位      更新时间:2023-09-26

我已经开始学习 ember.js 框架,但我被困在如何使用框架具有的 URL 类型功能的设置上。

http://emberjs.com/guides/routing/specifying-the-location-api/

我有这个简单的应用程序.js

App = Ember.Application.create();
App.Router.reopen({
    location: 'history'
});
App.Router.map(function () {
    this.route('about');
});
App.ApplicationRoute = Ember.Route.extend({
    model: function () {
        return appdata;
    }
});
App.IndexRoute = Ember.Route.extend({
    setupController: function (controller) {
        // Set the IndexController's `title`
        controller.set('indextitle', "My Index title");
    },
    renderTemplate: function () {
        this.render({ outlet: 'indexoutlet' });
    }
});
App.AboutRoute = Ember.Route.extend({
    model: function () {
        return appdata;
    },
    renderTemplate: function () {
        this.render({ outlet: 'aboutoutlet' });
    }
});
var appdata = { mytext: '', theplaceholder: 'Enter new text', attr:'Yeap!' }

如果我不使用

App.Router.reopen({
    location: 'history'
});

该应用程序工作正常,它通过附加 URL "~/EmberjsTest.aspx#/about"来进入"关于"路由。

但是,因为我不喜欢页面 URL 中的哈希符号,所以我宁愿将其删除,并且指南说我们应该放置以下代码:

App.Router.reopen({
    location: 'history'
});

但是当我这样做时,我在Chrome控制台中出现一个错误,说:"断言失败:URL '/EmberjsTest.aspx' 确实匹配了应用程序中的任何路由"

我做错了什么?

提前致谢

如果你想使用历史API,那么你有两个选择。

  1. '/'提供您的 Ember 应用程序,以便 Ember 可以使用其"正常"索引/根路由。

  2. 在 Ember 应用程序中创建一个可以处理'/EmberjsTest.aspx'的路由。

    this.route("index", { path: "/EmberjsTest.aspx" });

请注意,如果您使用选项 2,则可能需要更新所有路由以在其path中包含'/EmberjsTest.aspx'

this.resource("posts", {path: "/EmberjsTest.aspx/posts" })