Files
ClassContent/历届学生/吴欣阳/每天课程/2021-07-20/login.html
2024-09-27 02:06:13 +08:00

182 lines
4.0 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.router-link-active {
color: red;
}
</style>
</head>
<body>
<div class="app">
<p>
<router-link to="/home">首页</router-link>
<router-link to="/type">分类</router-link>
<router-link to="/car">购物车</router-link>
<router-link to="/me">我的</router-link>
</p>
<router-view></router-view>
</div>
</body>
<script src="vue.js"></script>
<script src="vue-router.js"></script>
<script>
const Home = {
template: `<div class="Home">首页</div>`
}
const Type = {
template: `<div class="Type">分类</div>`
}
const Car = {
template: `<div class="Car">购物车</div>`
}
const Me = {
template: `<div class="Me">我的</div>`
}
const Info = {
template: `<div class="Info">详情页</div>`
}
const Login = {
template: `<div class="Login">
<button @click="login">点我登录</button>
</div>`,
methods: {
login() {
localStorage.setItem("token", "123432345")
if (this.$route.query.hasOwnProperty("from")) {
console.log(this.$route.query.from);
this.$router.push({
path: this.$route.query.from
})
} else {
this.$router.back()
}
console.log();
}
}
}
const Reg = {
template: `<div class="Reg">
注册
</div>`
}
var routes = [
{
path: '/home',
component: Home,
meta: {
isLogin: false,
title: '首页'
}
}, // 首页
{
path: '/type',
component: Type,
meta: {
isLogin: false,
title: '分类'
}
}, // 分类
{
path: '/car',
component: Car,
meta: {
isLogin: true,
title: '购物车'
}
}, // 购物车
{
path: '/me',
component: Me,
meta: {
isLogin: true,
title: '我的'
}
}, // 我的
{
path: '/info',
component: Info,
meta: {
isLogin: false,
title: '详情页'
}
}, // 详情页
{
path: '/login',
component: Login,
meta: {
isLogin: false,
title: '登录'
}
}, // 登录
{
path: '/reg',
component: Reg,
meta: {
isLogin: false,
title: '注册'
}
}, // 注册
];
const router = new VueRouter({
routes // (缩写) 相当于 routes: routes
});
router.beforeEach((to, from, next) => {
document.title = to.meta.title
// 你访问的那个页面需要登录
if (to.meta.isLogin) {
// 再次判断,你是否登录
// 登录了给你看页面
// 没登陆不给看,并且强制去登录
// 表示未登录,
if (localStorage.getItem("token") == null) {
next({
path: '/login',
query: {
from: to.path
}
})
} else {
next()
}
} else {
next()
}
})
new Vue({
el: '.app',
router,
data() {
return {
}
}
})
</script>
</html>