【问题标题】:angularjs cordova base hrefangularjs科尔多瓦基地href
【发布时间】:2017-04-02 01:01:32
【问题描述】:

尝试设置正确的 base href 值以使 angularjs html5 网络应用程序在科尔多瓦中工作

最初使用$locationProvider.html5Mode(true)<base href="/">

  • 应用在普通浏览器中完美运行
  • cordova 给出 css / js / 模板等资源错误
    (我相信它会在 cordova 根目录而不是平台根目录中寻找资源?)

在 SO 和 ui-router FAQs 上尝试了一些替代方案:

<base href="."> 使用 html5 模式,根据this 答案:

  • 在 cordova 中发现资产正常(无资源错误)
  • ui-router 进入无限循环的尾部旋转,并出现以下错误消息
    Error: Failed to execute 'pushState' on 'History': A history state object with URL 'https://page/' cannot be created in a document with origin https://example.com

<base href="./">带html5模式:

  • 在 cordova 中找到资产(无资源错误)
  • 给我可怕的Error: 10 $digest() iterations reached. Aborting!消息 尝试使用 frankwallis solution 提到的 here 但这没有帮助(同样的错误)

没有base href$locationProvider.html5Mode({enabled: true, requireBase: false});

  • 在 cordova 中找到资产(无资源错误)
  • 页面开始在自己内部加载?? ...像 some 状态中的两次角引导?

我的应用配置定义如下:

myApp.config(['$locationProvider', '$urlRouterProvider', '$stateProvider', '$stickyStateProvider', function($locationProvider, $urlRouterProvider, $stateProvider, $stickyStateProvider) {

    $locationProvider.html5Mode(true);

    $stateProvider
    .state('root', {            // we define a 'root' state so that we can load some essential data prior to any template being loaded
        url: '',
        abstract: true,
        sticky: true,
        views: {
            'root': {
                template: '<ui-view/>',
            }
        }
    })
    .state('root.worldmap', {
        url : '/worldmap',
        templateUrl : 'templates/worldmap.tmpl.html'
    })
    .state('root.faq', {
        url : '/faq',
        templateUrl : 'templates/faq.tmpl.html'
    })
    .state("otherwise", {
        url: "*path",
        views: {
            'root': {
                template: "",
                controller: ['$injector', function($injector) {
                    var $state = $injector.get("$state");
                    $state.go('root.worldmap');
                }]
            }
        },
    });
    $stickyStateProvider.enableDebug(true);
}]);

有关信息: 分别使用替代科尔多瓦devicereadythis method vs angular.element引导用于科尔多瓦/浏览器

【问题讨论】:

    标签: angularjs html cordova angular-ui-router angular-ui-router-extras


    【解决方案1】:

    好的,所以让这个工作的方法是删除基本 href AND 禁用 html5 模式:

    • [可选] 创建并签出一个名为 dev-cordova 或类似名称的新分支
      • 这使您可以在浏览器和设备代码之间快速切换
    • 删除/评论主&lt;base href="/"&gt;中的index.html
    • 确保 html5 模式在app.js 中被禁用

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

      $locationProvider.html5Mode(false);
      

    别忘了运行 cordova build ios(在你 grunt / gulp 等编译 js 文件之后)
    ...这可确保更新 cordova /platforms/ios/www 目录中的文件。

    【讨论】:

    • 图片没有为我加载。我正在使用路径 ./images/logo.png 和 images/logo.png。有什么想法吗?
    猜你喜欢
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2015-01-25
    • 2016-11-21
    相关资源
    最近更新 更多