Vue代码分析
项目结构
-
打包完后的项目结构如图所示
-
/public目录下有 favicon.ico (标签图标),index.html (当前项目唯一的页面)
-
/src下的目录结构如图所示
assets目录
- assets里面的资源会被webpack打包进代码,static里面的资源就直接引用了(如下所示)
- 一般在static里放一些类库的文件,assets放属于项目的静态资源文件
components目录
/components用来存放小组件
Header.vue代码分析
//template,可以把列表项放入template标签中,然后进行批量渲染
<template>
//class属性代表i一个选择器,可以理解为一个标识用来识别特定标签
<div class="m-content">
<h3>欢迎来到MarkerHub的博客</h3>
<div class="block">
//el-avatar,头像组件
<el-avatar :size="50" :src="user.avatar"></el-avatar>
<div>{
{ user.username }}</div>
</div>
<div class="maction">
<span><el-link href="/blogs">主页</el-link></span>
<el-divider direction="vertical"></el-divider>
<span><el-link type="success" href="/blog/add">发表博客</el-link></span>
<el-divider direction="vertical"></el-divider>
<span v-show="!hasLogin"><el-link type="primary" href="/login">登录</el-link></span>
<span v-show="hasLogin"><el-link type="danger" @click="logout">退出</el-link></span>
</div>
</div>
</template>
//
<script>
export default {
//name用于指定该vue,该组件
name: "Header",
// $data: vue实例观察的数据对象,Vue实例代理了对其data对象属性的访问
data() {
//使用return包裹的数据指挥在当前组件中生效
return {
user: {
username: '请先登录',
avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
},
hasLogin: false
}
},
methods: {
logout() {
const _this = this
_this.$axios.get("/logout", {
headers: {
"Authorization": localStorage.getItem("token")
}
}).then(res => {
_this.$store.commit("REMOVE_INFO")
_this.$router.push("/login")
})
}
},
created() {
if(this.$store.getters.getUser.username) {
this.user.username = this.$store.getters.getUser.username
this.user.avatar = this.$store.getters.getUser.avatar
this.hasLogin = true
}
}
}
</script>
<style scoped>
.m-content {
max-width: 960px;
margin: 0 auto;
text-align: center;
}
.maction {
margin: 10px 0;
}
</style>
-
//template,可以把列表项放入template标签中,然后进行批量渲染
-
/class属性代表一个选择器,可以理解为一个标识用来识别特定标签
-
< scrtpt>
- $data: vue实例观察的数据对象,Vue实例代理了对其data对象属性的访问
- $el: vue实例使用的根DOM元素
- methods:标签绑定的事件函数
- created() 生命周期方法
代码分析
-
Header.vue的代码比较简单,一个标题标签显示欢迎内容,一个头像组件显示头像,一个小标题标签显示名字;用两个el-divider组件分隔开了三个el-link文字标签
-
主页标签的超链接是/blogs 博客列表
-
发表文章的超链接是/blogs/add
-
退出这个超链接,在!haslogin时显示为登录,绑定的请求是/login,haslogin时绑定的超链接是/logout
-
值得注意的是
methods: { logout() { const _this = this _this.$axios.get("/logout", { headers: { "Authorization": localStorage.getItem("token") } }).then(res => { _this.$store.commit("REMOVE_INFO") _this.$router.push("/login") }) } }, - 这个方法中,通过axios发出get请求,在headers中设置了一个字段Authorization": localStorage.getItem("token“) - 发送完请求后再store执行REMOVE_INFO,删除掉用户信息,再页面路由到登录界面
router目录
存放路由脚本文件(配置路由 url链接 与 页面组件的映射关系)
index.js
vue-router配置路由,使用vue的异步组件技术,可以实现按需加载
每一个路由规则都是一个对象
- path表示监听哪一个路由链接地址
- componrnt,表示路由是前面匹配到的path,则显示component对应的那个组件
- name, 给这个页面路径定义一个名字,当在页面进行跳转的时候也可以使用名字跳转,具有唯一性
$route
表示当前的路由信息(表示一条路由),包含了当前URL解析得到的信息(包含当前路径,参数,query对象等)
-
1.$route.path** 字符串,对应当前路由的路径,总是解析为绝对路径,如"/foo/bar"。
-
2.$route.params** 一个 key/value 对象,包含了 动态片段 和 全匹配片段, 如果没有路由参数,就是一个空对象。
-
3. r o u t e . q u e r y ∗ ∗ 一 个 k e y / v a l u e 对 象 , 表 示 U R