2

我正在尝试在本机脚本中创建示例应用程序。我通过引用http://docs.telerik.com/devtools/nativescript-ui/Controls/Angular/SideDrawer/getting-started以下列方式将 RadSideDrawer 用于 sidemenu :

<RadSideDrawer [transition]="RevealTransition" #drawer>
  <StackLayout tkDrawerContent class="sideStackLayout">
      <StackLayout class="sideTitleStackLayout">
          <Label text="Navigation Menu"></Label>
      </StackLayout>
      <StackLayout class="sideStackLayout">
          <Label text="Primary" class="sideLabel sideLightGrayLabel"></Label>
          <Label text="Social" class="sideLabel"></Label>
          <Label text="Promotions" class="sideLabel"></Label>
          <Label text="Labels" class="sideLabel sideLightGrayLabel"></Label>
          <Label text="Important" class="sideLabel"></Label>
          <Label text="Starred" class="sideLabel"></Label>
          <Label text="Sent Mail" class="sideLabel"></Label>
          <Label text="Drafts" class="sideLabel"></Label>
      </StackLayout>
  </StackLayout>
  <StackLayout tkMainContent>
    <GridLayout rows="*">
    <page-router-outlet ></page-router-outlet>
    <ActivityIndicator #activityIndicator horizontalAlignment="center" verticalAlignment="center"  width="100" height="100" row="0"></ActivityIndicator>
  </GridLayout>
  </StackLayout>
</RadSideDrawer>

在我的 app.component.ts 中,我给出了:

import {RadSideDrawer} from "nativescript-telerik-ui/sidedrawer";

@ViewChild("drawer") drawer : ElementRef;

public toggleDrawer(){
        let drawer = <RadSideDrawer>this.drawer.nativeElement;
        drawer.toggleDrawerState();
    }

问题是,每当我尝试执行 toggleDrawer() 时,都会出现错误:

 drawer.toggleDrawerState not a function.

可能出了什么问题?我将 this.drawer 作为 [object Object] 并将 this.drawer.nativeElement 作为 ProxyViewContainer(5),这意味着它正在识别元素,但不能调用 toggleDrawerState();

现在样式也被扭曲了,它现在在主视图中列出了 tkDrawerContent,我无法看到我在 tkMainContent 中指定的原始页面内容。

4

1 回答 1

2

RadSideDrawer(在 N+Angular2 项目中)有一些需要考虑的事情。

1.) 你有一个特殊的类型导入

import { RadSideDrawerComponent, SideDrawerType } from "nativescript-telerik-ui-pro/sidedrawer/angular";

@ViewChild(RadSideDrawerComponent) public drawerComponent: RadSideDrawerComponent;
private drawer: SideDrawerType;

2.) 您需要在组件的构造函数中实现 ChangeDetectorRef

import { Component, ElementRef, ViewChild, Injectable, OnInit, ChangeDetectorRef } from "@angular/core";
.....
constructor(private page: Page, private _changeDetectionRef: ChangeDetectorRef) {
        super();
}

3.) 在 ngAfterViewInit 中使用 ChangeDetectorRef 像这样

ngAfterViewInit() {
    this.drawer = this.drawerComponent.sideDrawer;
    this._changeDetectionRef.detectChanges();
}

4.) 最后使用抽屉方法是微不足道的

public toggleDrawer() {
    this.drawer.toggleDrawerState();
}

.....
<Button text="TOGGLE DRAWER" (tap)=toggleDrawer()></Button>

可以在此处找到使用 RadSideDrawer 和 angular 的完整示例

于 2016-09-29T09:51:46.897 回答