vuejs2 - 具有 ServiceStack 路由回退和 Api 前缀的 Vue.js 路由器历史记录模式

标签 vuejs2 servicestack vue-router

  1. 每个客户端路由都以哈希开头。如何在 Vue Router 中启用历史记录模式而不干扰 api 路由?

  2. 此外,我不想以“/api”开始我的路线。客户端路由不适用于 Config.HandlerFactoryPath 设置为“api”的情况。有办法实现吗?

APPHOST

public class AppHost : AppHostBase
{
    private BackendSettings _settings;
    private IHostingEnvironment _environment;

    public AppHost(BackendSettings settings, IHostingEnvironment environment) : base("Template.Www", typeof(SurveyService).Assembly)
    {
        _settings = settings;
        _environment = environment;

        Routes                
            .Add<GetSurvey>("/api/survey/{id}", "GET");
    }

    public override void Configure(Container container)
    {
        bool isDevelopment = _environment.IsDevelopment();

        Plugins.Add(new TemplatePagesFeature());

        SetConfig(new HostConfig
        {
            AddRedirectParamsToQueryString = true,
            DebugMode = isDevelopment,
        });
    }
}

启动

public class Startup
{
    public IConfiguration Configuration { get; }
    public Startup(IConfiguration configuration) => Configuration = configuration;

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        var backendSettings = GetBackendSettings();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseStaticFiles();

        app.UseServiceStack(new AppHost(backendSettings, env));
    }

    private BackendSettings GetBackendSettings()
    {
        var settings = new BackendSettings();
        Configuration.GetSection("Backend").Bind(settings);
        return settings;
    }

    private FrontendSettings GetFrontendSettings()
    {
        var settings = new FrontendSettings();
        Configuration.GetSection("Frontend").Bind(settings);
        return settings;
    }
}

请求模型

[Exclude(Feature.Metadata)]
[FallbackRoute("/{PathInfo}", Matches = "AcceptsHtml")]
public class Fallback
{
    public string PathInfo { get; set; }
}

VUE路由器

export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      component: Home
    },
    {
      path: '/test',
      component: Test
    },
    {
      path: '*',
      redirect: '/'
    }
  ]
})

最佳答案

[FallbackRoute] 的目的是在不匹配的路由上返回主页,因此如果该路由仅在客户端上定义,则全页请求将返回主页,以便客户端路由可以处理路由。

不可能有匹配的服务器路由和客户端路由,因为客户端路由需要服务器上的回退处理程序来返回主页,而这仅在不匹配的请求上发生。

关于vuejs2 - 具有 ServiceStack 路由回退和 Api 前缀的 Vue.js 路由器历史记录模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52096652/

相关文章:

javascript - Vuex 计算属性仅在路由更改然后更改回来时更新

javascript - Vue router 如何在页面加载时获取懒加载模块的当前路由路径?

javascript - 如何使用点击事件导航到另一个页面

javascript - axios ajax,发出ajax请求时显示加载

javascript - 在javascript中对数组的索引进行排序

vue.js - VueJS List Transitions JavaScript Hooks 并没有被列表中的每个元素调用

servicestack - 我可以让它在服务堆栈上自定义我的 GET 请求的显示吗?

asp.net-mvc - ServiceStack 如何在 MyController<T> 上 Autowiring 自定义 IRepository<T> 属性

mysql - 在 Mysql 中使用 OrmLite SqlExpression 时检测到潜在的非法片段?

javascript - Vue.js 服务端渲染中的组件