简体   繁体   English

Angular JS - 非单页SPA的路径

[英]Angular JS - Route to Non Single Page SPA

We need to load some pages in non SPA mode, while others in SPA mode. 我们需要在非SPA模式下加载一些页面,而在SPA模式下加载其他页面。 For example, homepage and about pages should be non SPA, while other pages should be SPA. 例如,主页和关于页面应该是非SPA,而其他页面应该是SPA。

$locationProvider.html5Mode(true); $ locationProvider.html5Mode(真); makes all pages SPA, even though route is not found. 即使找不到路由,也会使所有页面都SPA。 Not adding that line makes all pages non SPA. 不添加该行会使所有页面都不是SPA。

How do I set up routing so that if a route is not present the page should be fetched from the server? 如何设置路由,以便在路由不存在时,应从服务器获取页面? ie not be pushstate. 即不是pushstate。

Also, we're using ui-router. 另外,我们正在使用ui-router。

Using ui-router, you can have an otherwise state. 使用ui-router,您可以拥有其他状态。 In that otherwise state, you can add a resolve that can do a request to the server to handle the routing : 在其他状态下,您可以添加可以向服务器请求处理路由的解析:

$urlRouterProvider.otherwise("otherwise");

$stateProvider
  .state('otherwise', {
    url: '/otherwise',
    template: 'some html',
    resolve : {
      serverPage: function servePage() {
        //http request to the server to fetch and redirect. 
      }
    }
  })

not sure why the earlier answer with an 'otherwise' state did not work, since that seemed promising. 不确定为什么早期以“其他”状态回答的问题不起作用,因为这似乎很有希望。 if you have multiple non-spa pages to go to, and you want to manage all of that in routes, you can also just pass in a function to otherwise(), rather than define a separate state, like so: 如果您要访问多个非spa页面,并且想要在路由中管理所有这些页面,您也可以将函数传递给otherwise(),而不是定义单独的状态,如下所示:

$urlRouterProvider.otherwise(function($injector, $location){
    // redirect here, for example…
    window.location.href= window.location.origin + $location.url();
});

So that if the user navigates to the following route 因此,如果用户导航到以下路线

http://mydomain.com/spa#/nonspa http://mydomain.com/spa#/nonspa

It would be redirected to 它将被重定向到

http://mydomain.com/nonspa http://mydomain.com/nonspa

(assuming 'nonspa' in not a valid route) (假设'nonspa'不在有效路线中)

That said, I might just do a direct link to the non-spa page instead of going through the routing code -- seems simpler and faster. 也就是说,我可能只是直接链接到非spa页面而不是通过路由代码 - 看起来更简单,更快。

将target =“_ self”属性添加到锚标记是否允许您从SPA页面转到非SPA页面?

OK, so I tweaked the previous answers and found a workaround 好的,所以我调整了之前的答案并找到了解决方法

Basically, when the server is called for an SPA or non SPA page, the page needs to somehow indicate what mode it is running in. I choose a javascript variable for that 基本上,当为SPA或非SPA页面调用服务器时,页面需要以某种方式指示它正在运行的模式。我为此选择了一个javascript变量

var html5mode = true;// for SPA, set to false for non SPA. Emit this from the server.

In your routedefs.js or where ever you have the routes defined. 在您的routedefs.js或您定义路线的地方。

// enable html5Mode for pushstate ('#'-less URLs)
if (html5mode) {
     $urlRouterProvider.otherwise(function ($injector, $location) {
         // reload if route not found.
         window.location = window.location;
     });

     $locationProvider.html5Mode(true);
} else {
     $locationProvider.html5Mode(false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM