NativeScript:toggleDrawerState 不是函数错误 [英] NativeScript: toggleDrawerState not a function error

查看:24
本文介绍了NativeScript:toggleDrawerState 不是函数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用本机脚本创建示例应用程序.我通过参考 http://docs.telerik.com/devtools/nativescript-ui/Controls/Angular/SideDrawer/getting-started :

I'm trying to create a sample application in native script. I used RadSideDrawer for sidemenu in the following way by referencing http://docs.telerik.com/devtools/nativescript-ui/Controls/Angular/SideDrawer/getting-started :

<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 中,我给出了:

In my app.component.ts, I've given :

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

@ViewChild("drawer") drawer : ElementRef;

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

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

The problem is that whenever I'm trying to execute toggleDrawer(), I'm getting an error :

 drawer.toggleDrawerState not a function.

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

What could be gone wrong? I'm getting a this.drawer as an [object Object] and this.drawer.nativeElement as ProxyViewContainer(5), that means it is identifying the element, but cannot call toggleDrawerState();

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

Also the style is distorted now, it is now listing the tkDrawerContent in the main view and I'm not able to see the original page contents I specified in tkMainContent.

推荐答案

使用 RadSideDrawer(在 N+Angular2 项目中)需要考虑一些事项.

There are some things to consider with RadSideDrawer (in N+Angular2 project).

1.) 你有一个特殊的输入输入

1.) You have a special import for typings

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

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

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

2.) You need to implement ChangeDetectorRef in the constructor of your component

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

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

3.) In ngAfterViewInit use the ChangeDetectorRef like this

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

4.) 最后使用drawer方法很简单

4.) Finally uses the drawer method is trivial

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

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

可以找到使用带有 angular 的 RadSideDrawer 的完整示例 这里

The full example for using RadSideDrawer with angular can be found here

这篇关于NativeScript:toggleDrawerState 不是函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆