路线:
import vQuestion from './components/visitor/vQuestion';
import login from './components/auth/login';
import register from './components/auth/register';
import forgot from './components/auth/forgot';
import Home from './components/Home';
import Question from './components/Question/Question';
import Answer from './components/Answer/Answer';
export const routes = [
{ path: '/', name: 'vQuestion', component: vQuestion },
{ path: '/register', name: 'register', component: register },
{ path: '/login', name: 'login', component: login },
{ path: '/forgot', name: 'forgot', component: forgot },
{ path: '/answer/:id', name: 'answer', component: Answer},
{ path: '/home', name: 'home', component: Home, meta: {requiresAuth: true}},
{ path: '/question', name:'question', component: Question, meta: {requiresAuth: true} },
];
我想设置答案是是否已登录
<ul v-for="question in questions" v-bind:key="question.main" id="my-table">
<router-link class="" :to="{name:'answer', params: {id: question.main}}">
<p>{{question.title}}</p>
</router-link>
</ul>
但没有登录用户可以打开此路径,登录后当用户单击此链接时,时间路径将不会重定向
在main.js文件中
const router = new VueRouter({
mode: 'history',
routes
});
router.beforeEach((to,from,next) => {
const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
const currentUser = firebase.auth().currentUser;
if(requiresAuth && !currentUser) next('/');
else if(!requiresAuth && currentUser) next('/home');
else next();
});