【问题标题】:Angular 2.0 router navigation not working on iOS WKWebViewAngular 2.0 路由器导航在 iOS WKWebView 上不起作用
【发布时间】:2016-11-10 23:00:51
【问题描述】:

由于一些性能问题,我正在尝试升级 angularJS2/phonegap 应用以在 iOS 上使用 WKWebView。

很遗憾,任何路由导航调用都不起作用。这包括routerlinkthis.route.navigate 调用。没有抛出错误。有没有其他人看到过这个和/或可能有解决方法?

代码使用普通的 UIWebView 可以正常工作。

我是 Angular 的新手,欢迎提出任何建议。

下面是一些相关代码的样子:

import { Component } from "@angular/core";
import { Routes, Router, ActivatedRoute } from "@angular/router";
import { LoggedInCallback } from "./service/cognito.service";

export class HomeComponent implements LoggedInCallback {

constructor(public router:Router){
}

isLoggedIn(message:string, isLoggedIn:boolean) {
    if (isLoggedIn){
      this.router.navigate(['/home/cl']);
    }
    else {
      console.log('HomeComponent: '+message);
    }
}

路由模块:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { Routes, RouterModule } from '@angular/router';
import { CategoryListComponent } from './categorylist/categorylist.component';


const approutes: Routes = [
    {
         path: 'home/cl',
         component: CategoryListComponent
    },
    ...
];
@NgModule({
  declarations: [

 ],
  imports: [RouterModule.forRoot(approutes), 
        BrowserModule, 
        FormsModule],
  exports: [RouterModule]

})
export class AppRoutingModule { }

回应以下评论:

如前所述,这是一个 phonegap 应用程序,因此大多数参考文献都使用(我假设)文件:协议。但是,第一个页面可以正常加载,并且它引用了单个 JavaScript 文件中的内容。奇怪的是,所有其他路由器引用的内容也在同一个 JavaScript 文件中。

我希望有人能理解路由器行为的具体细节,以解释为什么它在这种环境中不起作用。

【问题讨论】:

  • 请给我看你的代码。
  • 如果您使用子路由,则需要导入 RouterModule。请添加一些代码,以便我们为您提供帮助。
  • 我已经添加了一些代码,但关键是它在 UIWebView 中运行良好,但在 WKWebView 中失败。
  • 对于那些使用cordova + reactjs(或angular)的人,请查看cordova-plugin-wkwebview-enginecordova-plugin-wkwebviewxhrfix
  • 我在 2020 年,你有想过吗?

标签: angular cordova angular-ui-router wkwebview


【解决方案1】:

您是通过网络服务器还是 file:// 协议访问它? wkwebview 似乎对此有问题。

See this article

【讨论】:

  • @Mike 如果您查看它提到的文章 Cordova 加载索引页面然后什么也没有发生“在 iOS 9 中加载索引页面 Apple 解决了您无法在 WKWebView 中加载 HTML 文档的问题。这是 Cordova 能够在 iOS 9 中添加对 WKWebView 的官方支持的主要原因。所以你的索引页面出现在 WKWebView 中,然后......没有其他工作。 “WKWebView:它似乎将本地文件视为来自远程服务器,即使它们位于应用程序本身中,并且此类请求被阻止。”由于 PhoneGap 是 Cordova,请尝试安装本地服务器。
  • 我确实阅读了这篇文章并且我对这个问题很熟悉。但是,它不应该适用,因为所有 .js 都在一个文件中,并且该文件已经加载。我希望那里有人对路由器行为有一些内部知识,并且可以解释正在发生的事情。不过,我非常感谢您的帮助...
  • @MikeM 看看这个:stackoverflow.com/a/28676439/3481709 好像你需要使用 loadFileURL
  • 如果您无法访问提到的文章,请使用此链接访问construct.net/en/blogs/ashleys-blog-2/…
猜你喜欢
  • 2017-04-06
  • 1970-01-01
  • 2016-04-24
  • 2019-02-20
  • 1970-01-01
  • 2018-04-10
  • 2018-07-04
  • 2019-08-01
相关资源
最近更新 更多