2

我有这个...

 this.router.navigate(['search', this.searchParameters]);

这会生成类似 '/search;text=test;dateFrom=12-10-2016'

这很好用!

但是...如果我刷新浏览器或在 URL 地址栏上按回车,我将得到 404...

无法获取 /search;text=test;dateFrom=12-10-2016

使用 angular 2 beta 可以正常工作,因为 beta 使用的是标准查询字符串格式,例如 ?text=test&dateFrom=12-10-2016。

现在,最终的 Angular 2 使用 Url Matrix Notation 来处理来自 URL 的参数。

关于如何使这项工作的任何线索?

不确定它是否有帮助,但这是我的 app.routing.ts

import { ModuleWithProviders }  from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent }      from './components/pages/home/home.component';
import { SearchComponent }      from './components/pages/search/search.component';
const appRoutes: Routes = [
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
},
{
path: 'search',
component: SearchComponent
}
];

导出常量路由:ModuleWithProviders = RouterModule.forRoot(appRoutes);

另外,我的 gulp/browserSync 上有这个...

browserSync.init({
   server: {
        baseDir: "./",
        routes: {
            "/home": "index.html",
            "/search": "index.html"
        }
    }
});
4

3 回答 3

0

你可以试试这个......这对我有用,唯一认为改变的是url......使用这种方法,url就像https://yoururl.com/#/page

https://stackoverflow.com/a/39103122

于 2016-10-14T18:31:43.740 回答
0

艾萨克给了我一些关于如何解决这种情况的线索。所以,这可以解决问题。

browserSync.init({
   server: {
        baseDir: "./",
        routes: {
            "/home": "index.html",
            "/search": "index.html"
        },
        middleware: function(req, res, next) {
            req.url = req.url.replace(';','?');
            req.url = req.url.replace(/;/g,'&');
            return next();
        }
    }
});
于 2016-10-12T18:55:23.420 回答
-1

当使用 Angular2(或任何 SPA)路由时,您真的希望 ANY 路由返回您的索引文件。对于不同类型的服务器,这将需要不同的解决方案。看起来 BrowserSync 没有内置功能,但这里列出了一些好的解决方案。最简单的实现可能是

var fs = require("fs"),
    path = require("path"),
    url = require("url"),
    gulp = require("gulp"),
    browserSync = require("browser-sync");

// The default file if the file/path is not found
var defaultFile = "index.html"

// I had to resolve to the previous folder, because this task lives inside a ./tasks folder
// If that's not your case, just use `__dirname`
var folder = path.resolve(__dirname, "../");

browserSync({
        files: ["./css/*.css", "./js/*.js", "./index.html"],
        server: {
            baseDir: "./",
            middleware: function(req, res, next) {
                var fileName = url.parse(req.url);
                fileName = fileName.href.split(fileName.search).join("");
                var fileExists = fs.existsSync(folder + fileName);
                if (!fileExists && fileName.indexOf("browser-sync-client") < 0) {
                    req.url = "/" + defaultFile;
                }
                return next();
            }
        }
    });
于 2016-10-12T15:54:19.297 回答