graph TB
A(父组件 beforeCreated) --> B(父组件 created) --> C(父组件 beforeMounted) --> D(子组件 beforeCreated) --> E(子组件 created) --> F(子组件 beforeMounted) --> G(子组件 mounted) --> H(父组件 mounted)

组件的调用顺序都是先父后子,渲染完成的顺序是先子后父。

组件的销毁操作是先父后子,销毁完成的顺序是先子后父。

加载渲染过程

父 beforeCreate->父 created->父 beforeMount->子 beforeCreate->子 created->子 beforeMount- >子 mounted->父 mounted

子组件更新过程

父 beforeUpdate->子 beforeUpdate->子 updated->父 updated

父组件更新过程

父 beforeUpdate -> 父 updated

销毁过程

父 beforeDestroy->子 beforeDestroy->子 destroyed->父 destroyed

方案一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
getDescribe(id) {
// 直接调用$router.push 实现携带参数的跳转
this.$router.push({
path: `/describe/${id}`,
})
}

// 方案一,需要对应路由配置如下:
{
path: '/describe/:id',
name: 'Describe',
component: Describe
}
// 很显然,需要在 path 中添加/:id 来对应 $router.push 中path携带的参数。
// 在子组件中可以使用来获取传递的参数值。
// $route.params.id

方案二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 父组件中:通过路由属性中的 name 来确定匹配的路由,通过 params 来传递参数。
this.$router.push({
name: 'Describe',
params: {
id: id
}
})

// 对应路由配置: 注意这里不能使用:/id来传递参数了,因为父组件中,已经使用params来携带参数了。
{
path: '/describe',
name: 'Describe',
component: Describe
}
//子组件中: 这样来获取参数
// $route.params.id

方案三:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 父组件:使用 path 来匹配路由,然后通过 query 来传递参数
// 这种情况下 query 传递的参数会显示在 url 后面?id=?
this.$router.push({
path: '/describe',
query: {
id: id
}
})

// 对应路由配置:
{
path: '/describe',
name: 'Describe',
component: Describe
}
// 对应子组件: 这样来获取参数
// $route.query.id
// 这里要特别注意 在子组件中 获取参数的时候是$route.params 而不是$router 这很重要~~~

刷新浏览器后,Vuex 的数据是否存在?如何解决?

在 vue 项目中用 vuex 来做全局的状态管理, 发现当刷新网页后,保存在 vuex 实例 store 里的数据会丢失。

原因:因为 store 里的数据是保存在运行内存中的,当页面刷新时,页面会重新加载 vue 实例,store 里面的数据就会被重新赋值初始化。

vuex

  1. State:

vuex 中的数据源,我们需要保存的数据就保存在这里,可以在页面通过 this.$store.state 来获取我们定义的数据;

  1. Getters:

Getter 相当于 vue 中的 computed 计算属性,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算,这里我们可以通过定义 vuex 的 Getter 来获取,Getters 可以用于监听、state 中的值的变化,返回计算后的结果

  1. Mutations:

需要修改 store 中的值唯一的方法就是提交 mutation 来修改

  1. Actions:

提交一个 actions,在 actions 中提交 mutation 再去修改状态值

  1. Module:

由于使用单一状态树,应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时,store 对象就有可能变得相当臃肿。
为了解决以上问题,Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块——从上至下进行同样方式的分割

数据状态持久化

用过 vuex 的肯定会有这样一个痛点,就是刷新以后 vuex 里面存储的 state 就会被浏览器释放掉,因为我们的 state 都是存储在内存中的。

vuex-persistedstate

通过 vuex-persistedstate 这个插件,来实现将数据存储到本地

  1. vuex 初始化就开始引入;
  2. 每次我们更新 vuex 的状态,localstorage 中的 vuex 也会随之改变;
  3. vuex-persistedstate 默认使用 localStorage 来固化数据。
1
npm install vuex-persistedstate
1
2
3
4
5
6
7
8
9
import createPersistedState from "vuex-persistedstate";
const store = new Vuex.Store({
modules: {
app,
user
},
getters,
plugins: [createPersistedState()] //加上这个就可以了
});

vuex-along

vuex-along 的实质也是将 vuex 中的数据存放到 localStorage 或者 sessionStroage 中,只不过这个存取过程组件会帮我们完成,我们只需要用 vuex 的读取数据方式操作就可以了

安装 vuex-along:

1
npm install vuex-along --save

配置 vuex-along: 在 store/index.js 中最后添加以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
import VueXAlong from "vuex-along"; //导入插件
export default new Vuex.Store({
//modules: {
//controler //模块化vuex
//},
plugins: [
VueXAlong({
name: "store", //存放在localStroage或者sessionStroage 中的名字
local: false, //是否存放在local中 false 不存放 如果存放按照下面session的配置
session: { list: [], isFilter: true } //如果值不为false 那么可以传递对象 其中 当isFilter设置为true时, list 数组中的值就会被过滤调,这些值不会存放在seesion或者local中
})
]
});

使用 localStorage 或者 sessionStroage

1
2
3
4
5
6
7
8
9
10
11
12
export default {
created() {
//在页面加载时读取 sessionStorage 里的状态信息
if (sessionStorage.getItem("store")) {
this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(sessionStorage.getItem("store"))));
}
//在页面刷新时将 vuex 里的信息保存到 sessionStorage 里
window.addEventListener("beforeunload", () => {
sessionStorage.setItem("store", JSON.stringify(this.$store.state));
});
}
};

自定义存储方式

使用 sessionStorage

1
2
3
4
5
const store = new Store({
// ...
plugins: [persistedState({ storage: window.sessionStorage })];
});

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { Store } from "vuex";
import createPersistedState from "vuex-persistedstate";
import * as Cookies from "js-cookie";
const store = new Store({
// ...
plugins: [
createPersistedState({
storage: {
getItem: (key) => Cookies.get(key),
setItem: (key, value) => Cookies.set(key, value, { expires: 3, secure: true }),
removeItem: (key) => Cookies.remove(key)
}
})
]
});

标题

总共有六级标题,分别为:

1
2
3
4
5
6
7
8
9
10
11
# 一级标题

## 二级标题

### 三级标题

#### 四级标题

##### 五级标题

###### 六级标题

注意:编写时最后一个#和标题名称之间有一个空格

列表

无序列表

- + *开头的文字

1
2
3
4
5
- 列表内容

* 列表内容

- 列表内容

注意:编写时- + *和列表内容之间有一个空格

有序列表

数字. 列表内容

1
2
1. 列表内容
2. 列表内容

注意:编写时.和列表内容之间有一个空格

引用

1
> 引用的内容

注意:编写时>和引用内容之间有一个空格

粗体和斜体

1
2
3
粗体: **需要加粗的文字**
斜体: _需要斜体的文字_
加粗并斜体: **_需要加粗和斜体的文字_**

下划线

1
++下划线文字++

删除线

1
~~删除线文字~~

文本高亮

1
==文本高亮==

图片和链接

1
2
图片: ![图片名称](图片地址)
链接: [链接名称](链接地址)

分割线

1
***或--- 代表分割线

代码框和代码块

1
2
3
4
5
6
7
1. 代码框: `代码框的内容`

2. 代码块:

```代码块所用编辑的语言名称
代码块内容
```

列表

此处以一个三行两列的表格为例:

1
2
3
4
| header 1    | header 2    |
| ----------- | ----------- |
| row 1 col 1 | row 1 col 2 |
| row 2 col 1 | row 2 col 2 |

可选框

1
2
未被选中的可选框:- [ ] 可选框后的内容
已被选中的可选框:- [x] 可选框后的内容

注意:-]之后都有一个空格,且未被选中的可选框格式中的[]之间有一个空格

添加目录

1
格式:在[]中添加 TOC 来自动添加目录

数学公式

1
2
3
```math
E = mc^3
```

流程图

指路:Mermaid绘图工具

搭建本地博客

准备工具:git,nodejs

Run git
选择进入一个博客放置目录

执行如下代码

1
2
3
4
npm install -g hexo
hexo init
hexo g
hexo s

打开浏览器输入:localhost:4000,就可以看到自己的本地博客

引用模板

如果不喜欢默认模板,可以引入其他主题模板
在 hexo 官网上选择喜欢的主题 ,克隆自己喜欢的模板
这里以 next 为例

执行如下代码

1
git clone https://github.com/next-theme/hexo-theme-next themes/next

删除主题文件中的.git
修改\_config.yml 配置文件 theme: next
模板配置,请 Google。

部署到 github

注册 github 账号
创建一个新项目,Repository name:你的 github 账号名.github.io(这里不区分大小写)
修改_config.yml 配置文件

1
2
3
4
deploy:
type: git
repository: 你的项目地址
branch: master

执行如下代码

1
2
3
npm install hexo-deployer-git --save
hexo g
hexo d

成功之输入后,在浏览器上输入:你的 github 账号名.github.io,就可以看到自己的博客

绑定域名

如果想要绑定自己的专属域名,可以去阿里云申请一个域名(申请域名是收费的)
地址解析:
记录类型:CNAME
主机记录:@
记录值:你的 github 账号名.github.io
然后在 source 目录下新建一个文件命名为:CNAME(不带后缀),内容为:你的域名(xxx.com)

执行如下代码

1
2
hexo g
hexo d

成功之后输入,在浏览器上输入:你的域名(xxx.com),就可以看到自己的博客

创建分类和标签

1
2
hexo new page categories //创建分类
hexo new page tags //创建标签

找到 categories 下的 index.md,编辑内容为:

1
2
3
4
---
title: 分类
type: categories
---

找到 tags 下的 index.md,编辑内容为:

1
2
3
4
---
title: 标签
type: tags
---

新文章页面都创建在_posts 目录下,使用 markdown 语法编辑
设置分类 categories: xxx
设置标签 tags: xxx
多个标签则用 JS 数组表示:[xxx,xxx]

因为 markdown 转 html 的原因,致使生成的 html 留有大量的空白,另外就是由于性能原因,需要对 js 和 css 进行压缩。

使用 hexo-neat 插件压缩

hexo-neat 插件使用 HTMLMinifier、clean-css、UglifyJS 插件实现。

安装 hexo-neat 插件

1
npm install hexo-neat --save

在站点 _config.yml 文件中增加如下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# hexo-neat

# 博文压缩

neat_enable: true

# 压缩 html

neat_html:
enable: true
exclude:

# 压缩 css

neat\*css:
enable: true
exclude: - '\*\*/\_.min.css'

# 压缩 js

neat_js:
enable: true
mangle: true
output:
compress:
exclude: - '**/\*.min.js' - '**/index.js'