Javascript 米特里尔:推迟路由配置,直到DOM就绪
我的应用程序视图中有div,它将用作我的应用程序页面的安装点Javascript 米特里尔:推迟路由配置,直到DOM就绪,javascript,mithril.js,Javascript,Mithril.js,我的应用程序视图中有div,它将用作我的应用程序页面的安装点 app.view = function(ctrl) { return [ appHeader.view(), appNav.view(), m("#page") ]; }; 以下内容似乎不起作用: m.mount(document.getElementById("app"), app); m.route.mode = "hash"; m.route(document.
app.view = function(ctrl) {
return [
appHeader.view(),
appNav.view(),
m("#page")
];
};
以下内容似乎不起作用:
m.mount(document.getElementById("app"), app);
m.route.mode = "hash";
m.route(document.getElementById("page"), "", {
"select_company": admin.SelectCompany
});
如果我将
直接包含在app.html中,它就会起作用。
如何在不直接编写html的情况下解决上述问题?有@ArthurClemens和@barneycarroll通过Gitter聊天告诉我,不建议在一个应用程序中同时使用m.mount()和m.route()。@barneycarroll提供的一个解决方案是只使用m.route(),并使用一个函数,该函数将返回页面视图以及应用程序的其他常见部分,如下面所示(jsbin为):
var header = {
view : function(){
return m( "h1", "This is the persistent site header" )
}
}
var nav = {
controller : function(){
this.links = [
[ "/", "Home" ],
[ "/select_company", "Companies" ]
]
},
view : function( ctrl ){
return m( "ul",
ctrl.links.map( function( link ){
return m( "li",
m( "a", {
config : m.route,
href : link[ 0 ]
}, link[ 1 ] )
)
} )
)
}
}
function furnish( component ){
return {
view : function(){
return [
header,
nav,
component
]
}
}
}
var home = {
view : function(){
return m( "h2", "Welcome!" )
}
}
var selectCompany = {
view : function(){
return m( "h2", "Please select a company" )
}
}
m.route.mode = "hash";
m.route( document.body, "/", {
"/" : furnish( home ),
"/select_company": furnish( selectCompany )
} );