【问题标题】:Vue.js router history mode with ServiceStack routing fallback and Api prefix带有 ServiceStack 路由回退和 Api 前缀的 Vue.js 路由器历史模式
【发布时间】:2018-08-30 12:04:54
【问题描述】:
  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: '/'
    }
  ]
})

【问题讨论】:

    标签: vuejs2 servicestack vue-router


    【解决方案1】:

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

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

    【讨论】:

    • 实际上,我不确定我是否完全理解您的回答。据我所见,我以与 ServiceStack Vue 模板中相同的方式配置了 [FallbackRoute]。为什么历史模式在该模板中有效,但在此处无效?
    • 我更新了我的 Vue 路由器。历史模式现在可以工作,但它的行为不像 SPA。每次更改路线时都会加载捆绑包。有什么帮助吗?
    • @glazjoon 我不知道这意味着什么,但您应该从一个 SPA 模板开始,它确实可以作为 SPA,请参阅vue-spa.web-templates.io的现场演示
    • 我的意思是 SPA 不应该在客户端导航上刷新。
    猜你喜欢
    • 2017-08-23
    • 2018-07-22
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2020-08-14
    • 1970-01-01
    相关资源
    最近更新 更多