ford04
ford04

Reputation: 74540

Nativescript: Cannot overwrite ActionBar in template

I am currently learning Nativescript and want to implement a sample Android app with Angular2.

When I route to a component that shall overwrite the default visible startpage ActionBar in the template with custom title and navigation button, nothing happens.

My Component with custom ActionBar:

@Component({
  template: `
  <ActionBar title="CustomTitle">
    <NavigationButton text="Back" android.systemIcon="ic_menu_back"></NavigationButton>
  </ActionBar>

   <StackLayout>
        <Label text="Hello World" textWrap="true"></Label>
        <page-router-outlet></page-router-outlet>
    </StackLayout>
  `
})
export class TestComponent {}

The title can only changed programmatically by page.actionBar.title = "MyGoals", but i dont want that in this case.

I tried to lean on Nativescript ActionBar Doc and Sample Groceries which seem to implement it similarly.

What must be done differently to change ActionBar in the template for a Android app?


Update

When i take this code and separate TestComponent in a feature module, the action bar does not work anymore for TestComponent - it shows the default title.

// main.ts
import { platformNativeScriptDynamic, NativeScriptModule } from "nativescript-angular/platform";
import { NgModule, Component } from "@angular/core";
import { Routes } from "@angular/router";
import { NativeScriptRouterModule } from "nativescript-angular/router";

@Component({
    template: `<page-router-outlet></page-router-outlet>`
})
export class AppComponent { }

@Component({
    template: `
        <ActionBar title="Home"></ActionBar>
        <StackLayout>
            <Label text="Home component" textWrap="true"></Label>
            <Button text="test" [nsRouterLink]="['/test']"></Button>
        </StackLayout>
    `
})
export class HomeComponent { }

@Component({
    template: `
        <ActionBar title="Test"></ActionBar>
        <StackLayout>
            <Label text="Test component" textWrap="true"></Label>
            <Button text="home" [nsRouterLink]="['/']"></Button>
        </StackLayout>
    `
})
export class TestComponent { }


const testRoutes: Routes = [
    {
        path: "test",
        component: TestComponent
    }
]

@NgModule({
    declarations: [TestComponent],
    imports: [
        NativeScriptRouterModule.forChild(testRoutes)
    ],
})
class TestComponentModule { }


const appRoutes: Routes = [
    {
        path: "",
        pathMatch: "full",
        component: HomeComponent
    }
]

@NgModule({
    declarations: [AppComponent, HomeComponent],
    bootstrap: [AppComponent],
    imports: [
        NativeScriptModule,
        NativeScriptRouterModule,
        NativeScriptRouterModule.forRoot(appRoutes),
        TestComponentModule
    ],
})
class AppComponentModule { }

platformNativeScriptDynamic().bootstrapModule(AppComponentModule);

Upvotes: 2

Views: 946

Answers (1)

Stanimira Vlaeva
Stanimira Vlaeva

Reputation: 81

there! Your configuration seems to be working. Although, the <page-router-outlet> tag doesn't make sense in the TestComponent. Here is a simple nativescript-angular app which correctly updates the actionbar:

// main.ts
import { platformNativeScriptDynamic, NativeScriptModule } from "nativescript-angular/platform";
import { NgModule, Component } from "@angular/core";
import { Routes } from "@angular/router";
import { NativeScriptRouterModule } from "nativescript-angular/router";

@Component({
    template: `<page-router-outlet></page-router-outlet>`
})
export class AppComponent { }

@Component({
    template: `
        <ActionBar title="Home"></ActionBar>
        <StackLayout>
            <Label text="Home component" textWrap="true"></Label>
            <Button text="test" [nsRouterLink]="['/test']"></Button>
        </StackLayout>
    `
})
export class HomeComponent { }

@Component({
    template: `
        <ActionBar title="Test"></ActionBar>
        <StackLayout>
            <Label text="Test component" textWrap="true"></Label>
            <Button text="home" [nsRouterLink]="['/']"></Button>
        </StackLayout>
    `
})
export class TestComponent {}

const routes: Routes = [
    {
        path: "",
        pathMatch: "full",
        component: HomeComponent
    },
    {
        path: "test",
        component: TestComponent
    }
]

@NgModule({
    declarations: [ AppComponent, HomeComponent, TestComponent ],
    bootstrap: [ AppComponent ],
    imports: [
        NativeScriptModule,
        NativeScriptRouterModule,
        NativeScriptRouterModule.forRoot(routes)
    ],
})
class AppComponentModule {}

platformNativeScriptDynamic().bootstrapModule(AppComponentModule);

Upvotes: 3

Related Questions