Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
leeguiman opened this issue Aug 23, 2017 · 10 comments
Open

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

leeguiman opened this issue Aug 23, 2017 · 10 comments

Comments

@leeguiman
Copy link

参考前辈的代码学习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,该操作结束当前代码块的生命周期
}
}

@myopenresources
Copy link
Owner

myopenresources commented Aug 23, 2017

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

@myopenresources
Copy link
Owner

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

@leeguiman
Copy link
Author

学习了

@zimv
Copy link

zimv commented Aug 27, 2017

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

@myopenresources
Copy link
Owner

啥意思?

@zimv
Copy link

zimv commented Aug 27, 2017

component里定义title属性,<title>标签,并没有被修改

@myopenresources
Copy link
Owner

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

@zimv
Copy link

zimv commented Aug 27, 2017

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

@myopenresources
Copy link
Owner

myopenresources commented Aug 27, 2017

@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
}
];

@yoonzm
Copy link

yoonzm commented Mar 12, 2018

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

https://segmentfault.com/a/1190000009971757

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants