Webview 导航上的Nativescript Web视图

Webview 导航上的Nativescript Web视图,webview,nativescript,uiwebviewdelegate,angular2-nativescript,Webview,Nativescript,Uiwebviewdelegate,Angular2 Nativescript,在nativescript中的webview上是否有“导航”事件 我在《沙马林》(c#)中用 Browser.Navigating += Myfunction nativescript中有类似的事件吗?我想这样做,如果我点击我的网站范围外的链接,那么我希望设备打开该url的网页,而不是在webview内导航 为此,您可以处理WebViewloadStartedEvent,并验证URL是否在您的站点范围内。如果不是,您可以为webview设置新的URL,这样您就可以阻止用户进入不同的网页。我附上

在nativescript中的webview上是否有“导航”事件

我在《沙马林》(c#)中用

Browser.Navigating += Myfunction

nativescript中有类似的事件吗?我想这样做,如果我点击我的网站范围外的链接,那么我希望设备打开该url的网页,而不是在webview内导航

为此,您可以处理
WebView
loadStartedEvent
,并验证URL是否在您的站点范围内。如果不是,您可以为webview设置新的URL,这样您就可以阻止用户进入不同的网页。我附上样本代码

main-page.xml

<Page xmlns:RL="nativescript-ripple" xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo">
 <GridLayout>
     <WebView src="https://www.google.bg/" id="wv" />

 </GridLayout>
</Page>

主页

import { EventData } from 'data/observable';
import { Page } from 'ui/page';
import { HelloWorldModel } from './main-view-model';
import {StackLayout} from "ui/layouts/stack-layout";
import {WebView, LoadEventData} from "ui/web-view"

// Event handler for Page "navigatingTo" event attached in main-page.xml
export function navigatingTo(args: EventData) {

  let page = <Page>args.object;

  var webview:WebView = <WebView> page.getViewById("wv");
    webview.on(WebView.loadStartedEvent, function (args: LoadEventData) {

        let message;
        console.log("url "+args.url.substring(0, 21));
        console.log("eventName "+args.eventName);
        console.log("navigationType "+args.navigationType);
        if(args.url.substring(0, 21) !== "https://www.google.bg/"){
          (<WebView>args.object).url="https://www.google.bg/";
          console.log("returns back");
        }
    });
}
从“数据/可观察”导入{EventData};
从'ui/Page'导入{Page};
从“/main view model”导入{HelloWorldModel};
从“ui/layouts/stack layout”导入{StackLayout};
从“ui/web视图”导入{WebView,LoadEventData}
//main-Page.xml中附加的页面“NavigationTo”事件的事件处理程序
导出函数导航到(参数:EventData){
让page=args.object;
var webview:webview=page.getViewById(“wv”);
on(webview.loadStartedEvent,函数(args:LoadEventData){
让信息;
log(“url”+args.url.substring(0,21));
console.log(“eventName”+args.eventName);
log(“navigationType”+args.navigationType);
如果(args.url.substring(0,21)!=”https://www.google.bg/"){
(args.object).url=”https://www.google.bg/";
console.log(“返回”);
}
});
}

希望这对使用NativeScript Vue的人有所帮助,我刚刚做了以下工作:

<template>
  <Page actionBarHidden="true">
    <WebView :src="html" @loadStarted="onLoadStarted" />
  </Page>
</template>

<script>
export default {
  methods: {
    onLoadStarted (event) {
      console.log('navigation detected with destination :', event.url)
    }
  }
}
</script>

导出默认值{
方法:{
onLoadStarted(事件){
console.log('检测到目标为“”的导航,event.url)
}
}
}

通过这种方式,您可以阻止导航并执行任何您想执行的操作:)

我不确定是否正确理解您的情况,但是我认为您可以使用
HtmlView
显示
URL
。当您点击该链接时,您将被重定向到设备浏览器<代码>事实并非如此。我有一个WebView,当我点击WebView中的链接时,我想检测该链接是否在WebView中我的站点范围内,或者它是一个外部链接,如果它是一个外部链接,我想打开一个新的浏览器。