cc icon indicating copy to clipboard operation
cc copied to clipboard

this.appService.titleEventEmitter.emit(item.name);

Open leeguiman opened this issue 6 years ago • 10 comments

参考前辈的代码学习angular4,在演练过程中感觉一个可以调整的地方: 把每个component中的构造函数constructor的类似代码this.appService.titleEventEmitter.emit("消息框")移除;在treeview-menu.component.ts 中实现: itemClicked(item: MenuData) { if (!this.isLeaf(item)) { item.isExpend = !item.isExpend; } else { this.appService.titleEventEmitter.emit(item.name);//新增代码,该行代码放在最后是不起作用的 this.router.navigate([item.url]); //this.appService.titleEventEmitter.emit(item.name); //代码在此处没有效果,个人猜测navigate类似return,该操作结束当前代码块的生命周期 } }

leeguiman avatar Aug 23 '17 10:08 leeguiman

1.如果要这么做就不需要服务了,直接通过main组件改变title。 2.如果把服务写在菜单的点击事件那,那么在点击菜单的时候是可以改变title,但是除了从菜单导航到某个页面,业务中还有可能是通过其他页面导航的,这样就没法改变title了,这样一来灵活性就不好了,还有一个致命的问题就是当你刷新页面或是直接输入地址进入时,title会没有值。

myopenresources avatar Aug 23 '17 13:08 myopenresources

所以我那样做是有我的考虑的

myopenresources avatar Aug 23 '17 13:08 myopenresources

学习了

leeguiman avatar Aug 24 '17 02:08 leeguiman

直接改title并不生效呢,然后我改造了一下,引入了platform-browser的Title,才真正改到标题了 constructor(private appService:AppService,private title:Title){ this.appService.titleEventEmitter.subscribe((value:string)=>{ if(value){ this.title.setTitle(value); } }) }

zimv avatar Aug 27 '17 03:08 zimv

啥意思?

myopenresources avatar Aug 27 '17 04:08 myopenresources

component里定义title属性,

标签,并没有被修改

zimv avatar Aug 27 '17 04:08 zimv

只要通过service设置值了,都可以用,刷新后一样还在

myopenresources avatar Aug 27 '17 05:08 myopenresources

app routes通过loadChildren载入了MainModule,app.module又注入了MainModule,这样可能会重复注册路由额,本来访问/app/home的,现在直接访问/home,你看路由是不是单独载入了home.compontent

zimv avatar Aug 27 '17 05:08 zimv

@zimv 谢谢提醒,之前做的时候没注意这个问题,我现在已经将路由重新定义了下,在AppModule中把LoginModule 与MainModule依赖去除了,都修改成loadChildren载入了,打包测试了下,也没有问题了,修改如下:

const appRoutes: Routes = [ { path: '', redirectTo: '/login', pathMatch: 'full' }, { path: 'login',
loadChildren: 'app/login/login.module#LoginModule' }, { path: 'app',
loadChildren: 'app/main/main.module#MainModule' },{ path:'**', component: PageNotFoundComponent } ];

myopenresources avatar Aug 27 '17 06:08 myopenresources

@myopenresources 你好,上面所说的改变title的方式,有没有考虑过使用监听angular中的路由事件,然后在配置路由的地方设置好所需数据(不限于title),监听到路由跳转成功后改变title即可,具体可查看此文章,亲测有效。

https://segmentfault.com/a/1190000009971757

yoonzm avatar Mar 12 '18 03:03 yoonzm