first commit

This commit is contained in:
编码猿
2024-09-27 02:06:13 +08:00
commit 852d94fbb9
36760 changed files with 3274413 additions and 0 deletions

View File

@@ -0,0 +1,149 @@
<!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://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id="app">
<p>
<!-- 使用 router-link 组件来导航. -->
<!-- 通过传入 `to` 属性指定链接. -->
<!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
<router-link to="/home">Go to Foo</router-link>
<router-link to="/me">Go to Bar</router-link>
</p>
<!-- 路由出口 -->
<!-- 路由匹配到的组件将渲染在这里 -->
<router-view></router-view>
</div>
</body>
<script>
// 0. 如果使用模块化机制编程导入Vue和VueRouter要调用 Vue.use(VueRouter)
// 1. 定义 (路由) 组件。
// 可以从其他文件 import 进来
const Home = {
data() {
return {
list: [
{ id: 1, title: 'ssssss1' },
{ id: 2, title: 'ssssss2' },
{ id: 3, title: 'ssssss3' },
]
}
},
methods: {
GotoMe() {
this.$router.push("/me")
// this.$router.push({ name: 'info', params: { id: 1 } })
}
},
template: `
<div>
<p @click="GotoMe">点我跳转</p>
<ul>
<li v-for="item,index in list" :key="index">
<router-link :to="{ name: 'info', params: { id: item.id } }">{{ item.title }}</router-link>
</li>
</ul>
</div>
`
}
const Me = {
template: '<div>bar</div>'
}
const info = {
data() {
return {
}
},
methods: {
},
template: `<div>详情页:{{ this.$route.params.id }}</div>`
}
const Login = () => {
return {
data() {
return {
}
},
methods: {
UserLogin() {
localStorage.setItem("token", '11111')
if (this.$route.query.hasOwnProperty("source")) {
this.$router.push({
path: this.$route.query.source
})
} else {
this.$router.push("/home")
}
}
},
template: `<div @click="UserLogin">登录页面</div>`
}
}
// 2. 定义路由
const routes = [
{ path: '/home', component: Home, meta: { isLogin: false, title: '首页' } },
{ path: '/me', component: Me, meta: { isLogin: true, title: '我的' } },
{ path: '/login', component: Login, meta: { isLogin: false, title: '登录' } },
{ path: '/info/:id', component: info, name: 'info', meta: { isLogin: false, title: '详情页' } },
]
// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
routes // (缩写) 相当于 routes: routes
});
router.beforeEach((to, from, next) => {
document.title = to.meta.title
console.log("你要去的页面:", to);
console.log("你离开的页面", from);
// 判断你要访问的页面是否需要登录
if (to.meta.isLogin) {
// 再次判断用户是否已经登录了
// 如果没有登录,那么去登录页面
// 如果已经登录,正常访问页面
// 登录过了
if (localStorage.getItem("token") !== null) {
next()
} else {
next({
path: '/login',
query: {
source: from.path
}
})
}
} else {
next()
}
})
// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
router
}).$mount('#app')
// 现在,应用已经启动了!
</script>
</html>