【问题标题】:Nativescript Angular ActionBar does not change between Child componentsNativescript Angular ActionBar 不会在子组件之间更改
【发布时间】:2017-11-24 01:10:09
【问题描述】:

在主组件中的 nativescript angular 项目中,我有两个子组件(TestComponent 和 Test1Component),每个子组件在两个布局中都有两个不同的操作栏实现。

我的问题是,当我在两个子组件之间导航时,视图中的操作栏不会改变。

但是,当我在主组件之外导航到另一个组件(Test2 组件)并从那里返回到两个子组件中的任何一个时,操作栏会正确显示。有人可以告诉我在两个子组件之间导航时如何让操作栏正确显示。

编辑:在实验中,我发现当我在 TestComponent 的操作栏中添加 SearchBar 时会发生这种行为。即使 Test1Component 中没有 searchBar - 搜索栏也会继续显示。

我的代码如下:

app.routing.ts

import { TestComponent } from "./test.component";
import { AppComponent } from "./app.component";
import { Test1Component } from "./test1.component";
import { Test2Component } from "./test2.component";

export const AppRoutes: any = [
    {
        path: "main", component: AppComponent,
        children: [
            { path: "0", component: TestComponent },
            { path: "1", component: Test1Component },
            { path: "", redirectTo: "0", pathMatch: "full" },
        ],
    },
    { path: "", redirectTo: "main", pathMatch: "full" },
    { path: "other", component: Test2Component, pathMatch: "full" },
];

Test 和 Test1 组件(2 个子组件)如下所示

import { Component } from '@angular/core';
import { Router } from "@angular/router";

@Component({
    selector: 'test1',
    templateUrl: `./test1.component.html`
        })
        export class Test1Component {

    constructor(private router: Router) {
        console.log("*********************** Now I am in test1 component *********************");
    }

    goToTest() {
        this.router.navigate(["/main/0"]);
    }

    goToOthers() {
        this.router.navigate(["/other"]);
    }
}

两个组件的 Html 布局相似,每个组件的操作栏布局不同

Test1Component.html

        <ActionBar title="Welcome to Test 1" backgroundColor="#f82462" color="white">
        </ActionBar>
   
    <StackLayout>
        <Label text="Hi," textWrap="true"></Label>
        <Label text="Hey I am Test-1, don't confuse with Test" textWrap="true"></Label>
        <Button text="Go To Test" (tap)="goToTest()"></Button>
        <Button text="Go To Other" (tap)="goToOthers()"></Button>
    </StackLayout>

    

最后父布局是这样的

<GridLayout rows="*, auto">
    <StackLayout row="0" orientation="vertical">
        <Label text="demo text"></Label>
        <router-outlet></router-outlet>
    </StackLayout>
</GridLayout>  

  

你也可以在我的githubrepo查看这个问题

请帮忙!

【问题讨论】:

  • 是否有理由将&lt;ActionBar&gt;&lt;/ActionBar&gt; 放在两个嵌套的StackLayout 中。我在 {N} + Angular 中看到的任何地方的 ActionBar 都在组件 HTML 的根目录中声明
  • @VladimirAmiorkov - 已删除嵌套的 StackLayouts 。如果 ActionBar 中没有任何内容,则 ActionBar 在页面之间导航时会发生变化。但是,当我在 TestComponent 的 ActionBar 中插入 SearchBar 时,它(SearchBar)继续显示在 Test1Component 中。
  • 我认为问题在于你有一个router-outlet 而不是一个页面路由器出口。我认为这就是 router-outlet 的工作方式,想象一个内部容器,您只需更改内容而不是创建全新的页面(导航)

标签: angular2-routing nativescript angular2-nativescript


【解决方案1】:

您似乎有两个“主要”页面。一个是“mains”,另一个是“app.component”。 即使您在路由中引导“mains”,将您的起始页指向“appComponent”。在那里你使用“路由器插座”。

我认为这就是你的问题所在。

【讨论】:

  • @MatanCp - 不确定我是否遵循 - Main 是我们的根组件,因此有一个 page-router-outlet。应用程序组件包含 2 个子组件(测试和测试 1),它们在其路由器插座中呈现。 Test2 组件是页面路由器出口中呈现的另一个组件。
  • 我不认为“mains”实际上被用作你的根组件。检查一下,在那里写一个“警报”或“控制台”,看看它是否被激活。我的根组件也在我的路由器中使用,就像在 app.module 的“引导程序”中一样。在您的情况下,您使用“appcomonent”作为路由器中的最低级别。
  • 刚刚检查过 - 电源上的控制台消息正在工作。在 repo 中也更新这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-26
  • 2018-07-05
  • 2023-02-03
  • 2020-01-02
  • 1970-01-01
相关资源
最近更新 更多