页面刷新的角度“无法获取"路线 [英] Angular 'Cannot GET' route on page refresh

查看:31
本文介绍了页面刷新的角度“无法获取"路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular应用程序中有一个搜索功能,当执行该功能时,它会击中我的API来获取结果,然后使用 $ location.url 重定向用户.一切正常...直到我尝试重新加载结果页面.这样做时,我会收到一条 Cannot GET/search/[search-term] 消息.

I have a search function within my Angular app that when executed hits my API to grab results then redirects the user using $location.url. Everything works great...until I try to reload the results page. When I do so I get a Cannot GET /search/[search-term] message.

app.config.js:

app.config.js:

'use strict';

angular.module('gameApp')
  .config(function($routeProvider, $locationProvider) {
    $routeProvider

    .when('/player/:playerId', {
      templateUrl: 'player.html'
    })
    .when('/search/:searchTerm', {
      templateUrl: '/app/gmSearchResults/gmSearchResults.html'
    });

    $locationProvider.html5Mode({
      enabled: true,
      requireBase: false,
      rewriteLinks: false
    });
  });

相关的navbar.controller.js:

relevant navbar.controller.js:

vm.getSearchResults = function(searchTerm) {
  searchService.resource.getResults({
    query: searchTerm
  }).$promise.then(function(results) {
    if (results.playerId) {
      $location.url('/customer/' + results.playerId);
    } else {
      saveLatest(results);
      $location.url('/search/' + searchTerm);
    }
  });
};

推荐答案

设置快递应用程序:

app.route('/*').get(function(req, res) { 
    return res.sendFile(path.join(config.root, 'index.html')); 
});

这篇关于页面刷新的角度“无法获取"路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆