订阅activatedRoute,params具有不同的属性来保存传递的参数值(subscribing to activatedRoute, params has different attributes to hold the param value passed)
参数在targetComponent中订阅:
import Router from @angular/Router export class TargetComponent { constructor(private activeRoute: ActivatedRoute) { this.activeRoute.params.subscribe(params => { console.log(params) // receives targetName but with different attributes console.log(params.name); // sometimes params has attribute name console.log(params.target); // and sometimes target }); } }
从firstComponent发送参数
import ActivatedRoute from @angular/Router this.router.navigate(['/', 'search' , targetName]);
发送参数形成第二部分
this.router.navigate(['/', 'table', targetName]);
路由配置
const routes: Routes = [ { path: '', redirectTo: 'welcome', pathMatch: 'full' }, { path: 'search/:name', component: SearchItemComponent }, { path: 'table/:target', component: TableDataComponent } ]
尝试了解为什么params对象在从不同组件导航时存储“值”属性并有时需要“名称”来存储值。
Params subscribe in targetComponent:
import Router from @angular/Router export class TargetComponent { constructor(private activeRoute: ActivatedRoute) { this.activeRoute.params.subscribe(params => { console.log(params) // receives targetName but with different attributes console.log(params.name); // sometimes params has attribute name console.log(params.target); // and sometimes target }); } }
Sending params from firstComponent
import ActivatedRoute from @angular/Router this.router.navigate(['/', 'search' , targetName]);
Sending params form secondcomponent
this.router.navigate(['/', 'table', targetName]);
Route Configuration
const routes: Routes = [ { path: '', redirectTo: 'welcome', pathMatch: 'full' }, { path: 'search/:name', component: SearchItemComponent }, { path: 'table/:target', component: TableDataComponent } ]
Trying to understand why the params object would have 'target' attribute and sometimes 'name' to store value when navigated from different components.
原文:https://stackoverflow.com/questions/48914576
最满意答案
您可以使用pagecontainer小部件的beforeshow事件: http ://api.jquerymobile.com/pagecontainer/#event-beforeshow
ui参数的toPage属性返回您要导航到的页面的jQuery对象。 然后,您可以检查其ID并显示正确的文本。
$(document).on( "pagecontainerbeforeshow", function( event, ui ) { var pageid = ui.toPage.prop("id"); if(pageid == 'frontPage') { setTitle('Front Page', ui.toPage); } else if (pageid == 'pageB') { setTitle('Page B', ui.toPage); } else if(pageid == 'pageC') { setTitle('Page C', ui.toPage); } }); function setTitle(text, page){ page.find(".headerText").text(text); }
每次导航到页面时都会运行,这样您每次都可以显示不同的标题。 如果您只显示相同的标题,则可以使用pagecreate事件,因为它只在每个页面最初加载到DOM时运行一次:
$(document).on( "pagecreate", function( e ) { var pageid = e.target.id; if (pageid == 'frontPage') { setTitle('Front Page', pageid); } else if (pageid == 'pageB') { setTitle('Page B', pageid); } else if(pageid == 'pageC') { setTitle('Page C', pageid); } }); function setTitle(text, pageid){ $("#" + pageid).find(".headerText").text(text); }
You can use the pagecontainer widget's beforeshow event: http://api.jquerymobile.com/pagecontainer/#event-beforeshow
The toPage property of the ui parameter returns a jQuery object of the page you are navigating to. You can then check its ID and display the correct text.
$(document).on( "pagecontainerbeforeshow", function( event, ui ) { var pageid = ui.toPage.prop("id"); if(pageid == 'frontPage') { setTitle('Front Page', ui.toPage); } else if (pageid == 'pageB') { setTitle('Page B', ui.toPage); } else if(pageid == 'pageC') { setTitle('Page C', ui.toPage); } }); function setTitle(text, page){ page.find(".headerText").text(text); }
This will run every time you navigate to the page so you could show different titles each time. If you only ever show the same title, you could use the pagecreate event instead as it only runs once when each page is initially loaded into the DOM:
$(document).on( "pagecreate", function( e ) { var pageid = e.target.id; if (pageid == 'frontPage') { setTitle('Front Page', pageid); } else if (pageid == 'pageB') { setTitle('Page B', pageid); } else if(pageid == 'pageC') { setTitle('Page C', pageid); } }); function setTitle(text, pageid){ $("#" + pageid).find(".headerText").text(text); }
相关问答
更多-
错误是一个label标签,其for属性不正确: 应该: 这是一个演示: http : //jsfiddle.net/zcUs2/ The error was a label tag that had a for attribute that was not correct: Should ...
-
你应该使用jQuery手机特定的方法。 $.mobile.changePage("Home.html"); 请参阅document-location-href-location-vs-mobile-changepagelocation Thanks Dipaks Instead of document.location.href = "/Home.html"; I have modified it as like below document.location.href = "./Home.html"; ...
-
显示页面加载Spinner on Ajax Call in jQuery Mobile(Show Page Loading Spinner on Ajax Call in jQuery Mobile)[2021-12-23]
您可以使用beforeSend并complete $.ajax事件来调用$.mobile.showPageLoadingMsg和$.mobile.hidePageLoadingMsg 。 看起来像这样: $('#main').live('pagecreate', function(event) { $.ajax({ beforeSend: function() { $.mobile.showPageLoadingMsg(); }, //Show spinner ... -
您可以使用pagecontainer小部件的beforeshow事件: http ://api.jquerymobile.com/pagecontainer/#event-beforeshow ui参数的toPage属性返回您要导航到的页面的jQuery对象。 然后,您可以检查其ID并显示正确的文本。 $(document).on( "pagecontainerbeforeshow", function( event, ui ) { var pageid = ui.toPage.prop("id") ...
-
在多个HTML文件的情况下, HEAD仅加载到第一个HTML文件中。 在其他文件中,只加载BODY内容。 这是因为AJAX用于将其他页面加载到DOM 。 由于原始DOM已经有HEAD内容,因此只能从其他页面加载主体。 如果您完全加载AJAX加载,或者如果初始化第一个HTML文件中的所有js文件,这可以被阻止。 如果你想了解更多,可以看看我的其他答案和其他几种解决方案,或者在这里找到答案 。 示例1:正确的方法 HTML 1:
... -
jQuery Mobile Force Reloading Page'加载页错误'(jQuery Mobile Force Reloading Page 'Error Loading Page')[2022-04-22]
您是否尝试使用两个页面,例如: yourpage.php和yourpageappcache.php以及yourpageappcache.php : I managed to solve the issue by disabling ajax before jQuery mobile has loaded. As in this answer: How To Disable Ajax In jQu ... -
下面这个脚本应放在你的jquery min和你的jquery mobile min之间。 否则它不会生效。 在关闭这些ajax功能的情况下向$ .mobile添加扩展程序可能有助于解决您的问题。 所有3个都不需要,但它们都适合您的问题。