尝试使用共享代码隐藏操作栏时出现问题

问题描述 投票:0回答:2

我已经构建了一个Angular + nativescript应用程序。我试图隐藏动作栏(像很多人一样)。

我已经做了两个服务助手。两者都有相同的签名。一个用于网络,另一个用于移动。对于移动助手,我处理我注入的TNS页面组件。

但是当我运行移动应用程序时,操作栏始终在这里。

Helpers

行动吧,helper.service.tns.ts:

import { Injectable } from "@angular/core";
import { Page } from "tns-core-modules/ui/page/page";
@Injectable()
export class ActionBarHelperService {  
    constructor(private page: Page){

    }

    hideActionBar(){
        this.page.actionBarHidden = true;
    }
}

行动吧,helper.service.ts:

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

@Injectable()
export  class ActionBarHelperService {    
    constructor(){}
    hideActionBar(){}
}

Shared component for mobile and web

login.component.ts:

@Component({
  selector: 'app-login',
  moduleId: module.id,
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent{

  constructor(private actionBarHelperService: ActionBarHelperService) 
  {
    this.actionBarHelperService.hideActionBar();
  }
}

另外,我在main.tns.ts中将createFrameOnBootstrap设置为true:

import { platformNativeScriptDynamic } from 'nativescript-angular/platform';
import { AppModule } from './app/app.module';

platformNativeScriptDynamic({ createFrameOnBootstrap: true }).bootstrapModule(AppModule);

我不知道是否有这个帮助,但是当我运行我的模拟器并且我没有将其设置为true时,我发出一个错误,表示该页面为null:

Successfully synced application org.nativescript.docdoc on device emulator-5554.
JS: Angular is running in the development mode. Call enableProdMode() to enable the production mode.
JS: ERROR TypeError: Cannot set property 'actionBarHidden' of null
JS: ERROR CONTEXT {
JS:   "view": {
JS:     "def": {
JS:       "nodeFlags": 33669121,
JS:       "rootNodeFlags": 33554433,
JS:       "nodeMatchedQueries": 0,
JS:       "flags": 0,
JS:       "nodes": [
JS:         {
JS:           "nodeIndex": 0,
JS:           "parent": null,
JS:           "renderParent": null,
JS:           "bindingIndex": 0,
JS:           "outputIndex": 0,
JS:           "checkIndex": 0,
JS:           "flags": 33554433,
JS:           "childFlags": 114688,
JS:           "directChildFlags": 114688,
JS:           "childMatchedQueries": 0,
JS:           "matchedQueries": {},
JS:           "matchedQueryIds": 0,
JS:           "references": {},
JS:           "ngContentIndex": null,
JS:           "childCount": 1,
JS:           "bindings": [],
JS:           "bindingFlags": 0,
JS:           "outputs": [],
JS:           "element": {
JS:             "ns": "",
JS:             "name": "app-login",
JS:             "attrs": [],
JS:             "template": null,
JS:             "componentProvider": {
JS:               "nodeIndex": 1,
JS:               "parent": "[Circular]",
JS:               "renderParent": "[Circular]",
JS:               "bindingIndex":...

任何想法或解决方案?

SOLUTION:

感谢@Manoj,我写了一个指令。见下文:

import { Directive } from '@angular/core';
import { Page } from 'tns-core-modules/ui/page/page';

@Directive({
  selector: '[hideActionBar]'
})
export class HideActionBarDirective {
  constructor(private page: Page) {
    this.page.actionBarHidden = true;
  }
}

现在在我的login.component.tns.html中:

<StackLayout hideActionBar>
    <Button text="login works!" class="btn btn-primary"></Button>
</StackLayout>
android ios angular nativescript angular7
2个回答
1
投票

由于ActionBarHelperServiceLoginComponent的依赖项,因此必须在创建页面之前实例化该服务。

也许你可以将页面作为参数传递给hideActionBar方法或者只是在visibility中的ActionBar标签上设置login.component.html属性或者使用指令代替服务。


0
投票

ActionBarHelperService期望构造函数中的参数(页面)(

constructor(private page: Page)

)但是当你通过以下方式在login.component.ts中创建它的实例时,在ActionBarHelperService中没有传递任何结果

constructor(private actionBarHelperService: ActionBarHelperService)

© www.soinside.com 2019 - 2024. All rights reserved.