2016-06-09 57 views
1

我想插入我的頁面上的WebView,並聽取了「loadFinishedEvent」 ...... 但做到這一點,我需要找到的WebView在我組件(TS文件),通過nativescript這樣我需要用一個標籤來包裝我的XML(UI):如何在angular2中使用nativescript的WebView?

<Page xmlns="http://schemas.nativescript.org/tns.xsd 

但這樣做我得到這個錯誤: 「類型錯誤:this.page.frame._getNavBarVisible不是一個函數」,並沒有頁面標記我已經嘗試了一些方法,但不成功...

我也沒有找到任何樣本,誰能幫助我?

回答

0

您是否嘗試過使用getViewById方法加載eventargs來查找控件。您可以在這裏找到更多的信息 - https://docs.nativescript.org/ui/ui-with-xml

+0

不行的,因爲 「角辦法」 的做... 我已經做解決它: <的WebView [SRC = 「URL」(loadFinished) =「loadFinished($ event)」> 刪除「Page」標籤(不應該與nativescript/Angular一起使用2 更多信息:[link](https://github.com/NativeScript/) nativescript-angular/issues/281) –

+0

@RichardSilveira您可以將您的評論擴展爲答案 – ahalls

1

良好的鏈接,文檔使用WebView Nativescript/Android

HTML:

<WebView #webview [src]="url"></WebView> 

後端代碼:

import {Component, OnInit} from "@angular/core"; 
import {WebView, LoadEventData} from "ui/web-view"; 

@Component({ 
    selector: 'basic-web-view-component', 
    templateUrl: //see html above 
}) 

export class BasicWebViewComponent implements OnInit { 

    public url="https://www.google.com"; 
    @ViewChild("webview") webview: WebView; 

    ngOnInit() { 

     this.webview.on(WebView.loadFinishedEvent, function (args: LoadEventData) { 
      console.log("loadFinishedEvent called"); 
     }); 
    } 
} 

注意:它似乎的WebView需求使用寬度/高度屬性在GridView中調整大小,請參閱上面的文檔參考。它不會基於內容動態增長。

+0

組件代碼不是後端代碼,而是前端代碼。 – Sebas

+0

是後端是一個不幸的短語......但HTNL fie背後的代碼文件通常稱爲後援代碼。什麼是更好的術語? – ahalls

+0

從我們的開發,我喜歡稱這種代碼爲「前端的後端」。這似乎有助於非開發人員瞭解Web前端的複雜性。 – Aaron

1

你的.ts文件,其中包含類實現

import { Component } from "@angular/core"; 

@Component({ 
selector: "Date", 
templateUrl: "myPage.html", //here is your reference to html page 
}) 

export class Angular2WebView { 
//any class function 
} 

HTML文件myPage.html下

<GridLayout> 
    <WebView id="webView" url="http://www.google.com" > 
    </WebView> 
</GridLayout> 

獲取更多信息。這裏指的Nativescript bare webview doesnt seem to work

1

我解決了它與下面的代碼:

<WebView [src]="url" (loadFinished)="loadFinished($event)"></WebView>

重要提示:刪除<Page>標籤(不應內Nativescript使用/角2)。

更多信息:github issue

+0

謝謝,還可以使用ViewChild裝飾器將自定義設置應用於不是原料的原生組件,不知道爲什麼。 –

相關問題