[英]Redirect route in meteorjs
所以我想在meteor.js中創建一個bit.ly類型的站點。 我不知道如何重定向頁面。 我將骨干網.js用於正在運行的路由。 理想情況下,它將從數據庫中獲取鏈接,創建鏈接並重定向到該鏈接。 我嘗試了window.location,但是無法正常運行js文件:
if (Meteor.isClient) {
var Router = Backbone.Router.extend({
routes: {
"" : "main",
"/" : "main",
"help" : "help",
'help/' : "help",
},
main: function() {
Session.set('currentPage', 'homePage');
},
help: function() {
Session.set('currentPage', 'helpPage');
}
});
var app = new Router;
Meteor.startup(function () {
Backbone.history.start({pushState: true});
});
Template.home.homePage = function(){
return Session.get("currentPage") == 'homePage';
};
Template.help.helpPage = function(){
return Session.get("currentPage") == 'helpPage';
//I want to do a redirect here somewhere:
//window.location = 'http://google.com';
};
}
的HTML:
<head>
<title>My app name</title>
</head>
<body>
{{> home}}
{{> help}}
</body>
<template name="home">
{{#if homePage}}
<h1>Home Page</h1>
{{/if}}
</template>
<template name="help">
{{#if helpPage}}
<h1>Help Page</h1>
{{/if}}
</template>
導航到外部URL。 您可以覆蓋路由器導航
if (Meteor.isClient) {
var Router = Backbone.Router.extend({
routes: {
"" : "main",
"help" : "help",
'about' : "about",
},
navigate: function (url) { window.location = url; },
main: function() {
Session.set('currentPage', 'homePage');
},
help: function() {
Session.set('currentPage', 'helpPage');
},
about: function() {
Session.set('currentPage', 'about');
}
});
var app;
Meteor.startup(function () {
app = new Router;
Backbone.history.start({pushState: true});
});
Template.content.homePage = function(){
return Session.get("currentPage") == 'homePage';
};
Template.content.helpPage = function(){
return Session.get("currentPage") == 'helpPage';
};
Template.content.aboutPage = function(){
return Session.get("currentPage") == 'about';
};
//I will add a helper below that will redirect to google everytime about link is clicked. Open console to see message logged before redirect
Template.about.rendered = function(){
console.log('About is about to be rendered but wait..... we redirect to google.com');
app.navigate('http://google.com');
};
采用
Backbone.history.navigate('/help', trueorfalse);
如果要運行更新會話變量的路由器方法,則為true
我已經修改了您的html模板,以幫助說明如何使用Backbone.history.navigate進行重定向。 我添加了兩個額外的模板,分別稱為“內容”和“關於”以及3個靜態鏈接,這些鏈接指向到“回家”,“幫助”和“關於”的三個路線。 內容取決於我們的模板助手主頁,helpPage或aboutPage返回的內容
<head>
<title>My app name</title>
</head>
<body>
<a href='/'>Home</a>
<a href='/help'>Help</a>
<a href='/about'>About</a>
{{> content}}
</body>
<template name='content'>
{{#if homePage}}
{{> home}}
{{/if}}
{{#if helpPage}}
{{> help}}
{{/if}}
{{#if aboutPage}}
{{> about}}
{{/if}}
</template>
<template name="home">
<h1>Home Page</h1>
</template>
<template name="help">
<h1>Help Page</h1>
</template>
<template name="about">
<h1>About Page</h1>
</template>
JS現在
if (Meteor.isClient) {
var Router = Backbone.Router.extend({
routes: {
"" : "main",
"help" : "help",
'about' : "about",
},
main: function() {
Session.set('currentPage', 'homePage');
},
help: function() {
Session.set('currentPage', 'helpPage');
},
about: function() {
Session.set('currentPage', 'about');
}
});
Meteor.startup(function () {
var app = new Router;
Backbone.history.start({pushState: true});
});
Template.content.homePage = function(){
return Session.get("currentPage") == 'homePage';
};
Template.content.helpPage = function(){
return Session.get("currentPage") == 'helpPage';
};
Template.content.aboutPage = function(){
return Session.get("currentPage") == 'about';
};
//I will add a helper below that will redirect to homePage everytime about link is clicked. Open console to see message logged before redirect
Template.about.rendered = function(){
console.log('About is about to be rendered but wait..... we redirect to home');
Backbone.history.navigate( "/", true);
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.