在Aurelia中激活后设置页面标题

gen*_*ser 1 router config page-title aurelia

我正在尝试设置标题,但是在下面的代码中它可以在一个地方工作而在其他地方不起作用.我想获得产品名称并设置标题.还有其他办法吗?

activate(params: any, route, navigationInstruction) {       
        //route.navModel.router.title="test"; //works here
         this.api.pull<Product>([params.id]).then(items => {
                items.forEach(item=>
                {
                    if(item.id == params.id)
                        route.navModel.router.title = item.name //does NOT work here
                });

         });
    }
Run Code Online (Sandbox Code Playgroud)

Mat*_*vis 6

尝试使用以下setTitle方法NavModel:

activate(params, routeConfig) {
  return this.api.pull<Product>([params.id]).then((items) => {
    let item = items.find((item) => item.id == params.id);
    if (item) {
        routeConfig.navModel.setTitle(item.name);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,您将下拉产品,然后将页面标题设置为项目的名称.在这个用例中,您可能想要设置navModel标题.但是,如果您确实要更改Router标题而不仅仅是当前的navModel,则可以执行以下操作:

import { inject, Router } from 'aurelia-framework';

@inject(Router)
export class MyViewModel

    constructor(router) {
        this.router = router;
    }

    activate(params, routeConfig) {
      return this.api.pull<Product>([params.id]).then((items) => {
        let item = items.find((item) => item.id == params.id);
        if (item) {
            this.router.title = item.name;
            this.router.updateTitle();
        }
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

请参阅路由器文档中的更多信息.