【问题标题】:Issue when trying to hide Action Bar with shared code尝试使用共享代码隐藏操作栏时出现问题
【发布时间】:2019-01-24 20:24:18
【问题描述】:

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

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

但是当我运行移动应用时,操作栏总是在这里。

助手

action-bar-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;
    }
}

action-bar-helper.service.ts:

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

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

移动和网络共享组件

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();
  }
}

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

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

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

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

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":...

有什么想法或解决方案吗?

解决方案:

感谢@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


    【解决方案1】:

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

    您可以将页面作为参数传递给hideActionBar 方法,或者只是在login.component.html 中的ActionBar 标记上设置visibility 属性,或者使用指令而不是服务。

    【讨论】:

    • 正如 Narendra Mongiya 提到的,我已经尝试将页面作为参数传递。对于第二种方式,我的 login.component.tns.html 中没有 ActionBar 标记,因为 Android 下默认设置了 ActionBar。但是,您使用指令的解决方案似乎很有趣……可以解决问题。我会尝试。谢谢
    • 您的解决方案效果很好。我没想到,但指令是一个简单而干净的解决方案。与模板交互甚至比服务更有意义。见上面我的编辑问题
    【解决方案2】:

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

    constructor(private page: Page)

    ) 但是当您通过以下方式在 login.component.ts 中创建它的实例时,ActionBarHelperService 中不会传递任何参数

    constructor(private actionBarHelperService: ActionBarHelperService)

    【讨论】:

    • 实际上,我已经尝试过这样做,但是由于我的登录组件是共享的,所以当我运行ng serve 时,angular cli 说它无法解析来自“tns-core-modules”的依赖页面/ui/page/page" 而此依赖项位于 nodes_modules 文件夹中...但是,tns run android 在这种情况下效果很好,并且操作栏被隐藏了。
    猜你喜欢
    • 2015-04-29
    • 1970-01-01
    • 2018-08-22
    • 2012-11-11
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 2016-05-31
    • 2012-12-06
    相关资源
    最近更新 更多