Files
2024-09-27 02:06:13 +08:00

132 lines
4.1 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<style>
.router-link-active {
color: red;
}
.fade-enter-active, .fade-leave-active {
transition: opacity .5s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
opacity: 0;
}
</style>
</head>
<body>
<div id="app">
<h1>Hello App!</h1>
<p>
<!-- 使用 router-link 组件来导航. -->
<!-- 通过传入 `to` 属性指定链接. -->
<!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
<router-link :to="{ name: 'zs' }">Go to Foo</router-link>
<!-- <router-link :to="{ path: '/bar', query: { url: '1222', page: '1' } }">Go to Bar</router-link> -->
<router-link :to="{ name: 'foo', params: { url: '1222', page: '1' } }">Go to Bar</router-link>
<router-link to="/me">我的</router-link>
</p>
<!-- 路由出口 -->
<!-- 路由匹配到的组件将渲染在这里 -->
<transition name="fade">
<router-view></router-view>
</transition>
</div>
</body>
<script>
// 0. 如果使用模块化机制编程导入Vue和VueRouter要调用 Vue.use(VueRouter)
// 1. 定义 (路由) 组件。
// 可以从其他文件 import 进来
const Foo = { template: '<div>foo</div>' }
const Login = { template: '<div>登录页面</div>' }
const Me = { template: '<div>我的页面</div>' }
const error = { template: '<div>404错误页面</div>' }
const Bar = {
data() {
return {
params: this.$route.params
}
},
created() {
// document.title = "详情页"
},
template: `
<div>详情 {{params.url}} {{params.page}}</div>
`,
}
// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
{
path: '*',
redirect: { name: 'error' }
},
{ path: '/User/foo', component: Foo, name: 'zs', meta: { title: '详情页', isLogin: false } },
{ path: '/Error', component: error, name: 'error', isLogin: false },
{ path: '/User/bar/:url/:page', component: Bar, name: 'foo', meta: { title: 'bar页面', isLogin: false } },
{ path: '/me', component: Me, name: 'me', meta: { title: '我的页面', isLogin: true } },
{ path: '/login', component: Login, name: 'login', meta: { title: '登录页面', isLogin: false } },
]
// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
routes // (缩写) 相当于 routes: routes
})
router.beforeEach(function(to, from, next) {
console.log("to: ", to);
// 需要登录才能查看的
if (to.meta.isLogin) {
// 登录过了
if (localStorage.getItem("token") !== null) {
next()
// 没有登录
} else {
next({
path: '/login',
query: {
fromPage: from.path
}
})
}
// 不需要登录
} else {
next()
}
document.title = to.meta.title
next()
})
// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
el: '#app',
router
})
</script>
</html>