Skip to content

Commit 5ffc6fa

Browse files
authored
docs: update vue.md (#832)
* doc:update docs/flutter.md * docs:update docs/flutter.md * docs:update docs/flutter.md * docs:update vue.md & flutter.md
1 parent ade6fbe commit 5ffc6fa

File tree

1 file changed

+352
-0
lines changed

1 file changed

+352
-0
lines changed

docs/vue.md

Lines changed: 352 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,358 @@ const value = inject(ProvideKey)
721721

722722
<!--rehype:className=wrap-text -->
723723

724+
## 路由
725+
726+
### 1.路由的基本使用
727+
728+
1.开启命名空间后,组件中读取state数据:
729+
730+
```javascript
731+
//方式一:自己直接读取
732+
this.$store.state.personAbout.list
733+
734+
//方式二:借助mapState读取:
735+
...mapState('countAbout',['sum','school','subject']),
736+
```
737+
738+
2.开启命名空间后,组件中读取getters数据:
739+
740+
```javascript
741+
//方式一:自己直接读取
742+
this.$store.getters['personAbout/firstPersonName']
743+
//方式二:借助mapGetters读取:
744+
...mapGetters('countAbout',['bigSum'])
745+
```
746+
747+
3.开启命名空间后,组件中调用dispatch
748+
749+
```javascript
750+
//方式一:自己直接dispatch
751+
this.$store.dispatch('personAbout/addPersonWang',person)
752+
//方式二:借助mapActions:
753+
...mapActions('countAbout',{incrementOdd:'jia0dd',incrementWait:'jiaWait'})
754+
```
755+
756+
4.开启命名空间后,组件中调用commit
757+
758+
```javascript
759+
//方式一:自己直接commit
760+
this.$store.commit('personAbout/ADD_PERSON',person)
761+
//方式二:借助mapMutations:
762+
...mapMutations('countAbout',{increment:'JIA',decrement:'JIAN'}),
763+
```
764+
765+
### 2.路由的使用
766+
767+
```javascript
768+
import VueRouter from 'vue-router'
769+
//引入Luyou 组件
770+
import About from '../components/About'
771+
import Home from '../components/Home'
772+
//创建router实例对象,去管理一组一组的路由规则
773+
const router = new VueRouter({
774+
routes:[
775+
path:'/about',
776+
component:About
777+
path:'/home',
778+
component:Home
779+
})
780+
//暴露router
781+
export default router
782+
```
783+
784+
4.实现切换(active-class可配置高亮样式)
785+
786+
\<router-link active-class="active" to="/about">About\</router-link>
787+
788+
5.指定展示位置
789+
790+
\<router-diew>\</router-view>
791+
792+
>几个注意点
793+
>
794+
>1.路由组件通常存放在pages文件夹,一般组件通常存放在components文件夹。
795+
>
796+
>2.通过切换,“隐藏”了的路由组件,默认是被销毁掉的,需要的时候再去挂载。
797+
>
798+
>3.每个组件都有自己的$route属性,里面存储着自己的路由信息。
799+
>
800+
>4.整个应用只有一个router,可以通过组件的srouter 属性获取到。
801+
802+
### 3.路由的query
803+
804+
```html
805+
<template>
806+
<div>
807+
<ul class="list">
808+
<!-- 跳转路由并携带参数 -->
809+
<!-- <li v-for="item of data" :key="item.id">
810+
<router-link
811+
class="link"
812+
:to="`/home/message/mes?id=${item.id}&title=${item.mes}`"
813+
>{{item.mes}}</router-link> -->
814+
<!-- to的对象写法 -->
815+
<li v-for="item of data" :key="item.id">
816+
<router-link
817+
class="link"
818+
:to="{
819+
path:'/home/message/mes',
820+
query:{
821+
id:item.id,
822+
title:item.mes
823+
}
824+
}"
825+
>{{item.mes}}</router-link>
826+
</li>
827+
</ul>
828+
<hr>
829+
<router-view></router-view>
830+
</div>
831+
</template>
832+
833+
<script>
834+
export default {
835+
name:'HomeChild1',
836+
data() {
837+
return {
838+
data:[
839+
{id:1,mes:"消息1"},
840+
{id:2,mes:"消息2"},
841+
{id:3,mes:"消息3"}
842+
]
843+
}
844+
},
845+
}
846+
</script>
847+
848+
<style scoped>
849+
.list{
850+
margin-left:80px;
851+
}
852+
.link{
853+
color: orange;
854+
text-decoration: none;
855+
background-color: skyblue;
856+
}
857+
</style>
858+
```
859+
860+
> 接收参数 `{{$route.query.id}}`
861+
862+
### 4.命名路由
863+
864+
```javascript
865+
routes:[
866+
{
867+
path:'/about',
868+
component:AboutBody
869+
},
870+
{
871+
path:'/home',
872+
component:HomeBody,
873+
children:[
874+
{
875+
path:'news',
876+
component:HomeChild
877+
},
878+
{
879+
path:'message',
880+
component:HomeChild1,
881+
//多级路由
882+
children:[
883+
{
884+
name:'richu',
885+
path:'mes',
886+
component:HomeMessage
887+
}
888+
]
889+
}
890+
]
891+
}
892+
]
893+
894+
```
895+
896+
> 使用
897+
>
898+
> :to="{
899+
> name:'',
900+
> path:'/home/message/mes',
901+
>
902+
> ​ query:{id:item.id,
903+
>
904+
> ​ title:item.mes
905+
>
906+
> ​ }
907+
>
908+
> }"
909+
910+
### 5.params参数的使用
911+
912+
1.声明接收
913+
914+
```javascript
915+
children:[
916+
{
917+
name:'richu',
918+
path:'mes/:id/:title',
919+
component:HomeMessage
920+
}
921+
]
922+
```
923+
924+
2.传递
925+
926+
```html
927+
<li v-for="item of data" :key="item.id">
928+
<router-link
929+
class="link"
930+
:to="`/home/message/mes/${item.id}/${item.mes}`"
931+
>{{item.mes}}
932+
</router-link>
933+
</li>
934+
```
935+
936+
3.接收
937+
938+
```html
939+
<li>编号{{$route.params.id}}</li>
940+
<li>标题{{$route.params.title}}</li>
941+
```
942+
943+
### 6.props的使用
944+
945+
```javascript
946+
7.路由的props配置
947+
作用:让路由组件更方便的收到参数
948+
name:'xiangqing',path:'detail/:id',component:Detail,
949+
//第一种写法:props值为对象,该对象中所有的key-value的组合最终都会通过props传给Detai1组件
950+
// props:{a:900]
951+
//第二种写法:props值为布尔值,布尔值为true,则把路由收到的所有params参数通过props传给Detai1组件
952+
// props:true
953+
//第三种写法:props值为函数,该函数返回的对象中每一组key-value都会通过props传给Detail组件
954+
props(route){
955+
return {
956+
id:route.query.id,
957+
title:route.query.title
958+
```
959+
960+
> \<router-link>的replace属性
961+
>
962+
> 1.作用:控制路由跳转时操作浏览器历史记录的模式
963+
>
964+
> 2.浏览器的历史记录有两种写入方式:分别为 push和replace,默认为push
965+
>
966+
> 3.如何开启replace 模式:
967+
>
968+
> push是追加历史记录,
969+
>
970+
> replace 是替换当前记录[路由跳转时候\<router-link replace>News\</router-link>]
971+
972+
### 7.编程式路由导航
973+
974+
1.作用:不借助router-link实现路由跳转,让跳转更加灵活
975+
2.具体编码:
976+
977+
```javascript
978+
//$router的两个API
979+
this.$router.push({
980+
name:'xiangqing',
981+
params:{
982+
id:xxx,
983+
title:xxx
984+
//实现路由跳转,让路由跳转更加灵活
985+
}
986+
})
987+
this.$router.replace({
988+
name:'xiangqing',
989+
params:{
990+
id:xxx,
991+
title:xxx
992+
}
993+
})
994+
this.$router.forward();
995+
this.$router.back();
996+
this.$router.go(3);
997+
```
998+
999+
### 8.缓存路由组件
1000+
1001+
> 让不展示的路由组件保持挂载,不被销毁
1002+
1003+
> 具体编码
1004+
1005+
```html
1006+
<keep-alive include="news">
1007+
<router-view></router-view>
1008+
</keep-alive>
1009+
```
1010+
1011+
> include里面写模块名,用于保存指定的模块
1012+
1013+
### 9.新生命周期钩子
1014+
1015+
> 作用:路由组件独有的,用于捕获路由组件的激活状态
1016+
1017+
activated 路由组件被激活时触发
1018+
1019+
deactivated 路由组件失活时触发
1020+
1021+
## 路由守卫
1022+
1023+
#### 1.前置路由守卫
1024+
1025+
```javascript
1026+
/* 前置路由 */
1027+
route.beforeEach((from,to,next)=>{
1028+
if (to.meta.isAuth){
1029+
alert("1");
1030+
next();
1031+
}else{
1032+
next();
1033+
}
1034+
})
1035+
```
1036+
1037+
#### 2.后置路由守卫
1038+
1039+
```javascript
1040+
/* 后置路由 */
1041+
route.afterEach((from,to)=>{
1042+
console.log(to);
1043+
document.title=from.meta.title;
1044+
})
1045+
```
1046+
1047+
#### 3.独享路由守卫
1048+
1049+
```javascript
1050+
{
1051+
path:'news',
1052+
component:HomeChild,
1053+
meta:{title:"新闻"},
1054+
beforeEnter: (from,to,next)=>{
1055+
1056+
}
1057+
},
1058+
```
1059+
1060+
> 独享路由守卫只有前置路由守卫没有后置路由守卫
1061+
1062+
#### 4.组件内路由守卫
1063+
1064+
```javascript
1065+
/* 通过路由规则,进入该组件时被调用 */
1066+
beforeRouteEnter (to, from, next) {
1067+
// ...
1068+
},
1069+
/* 通过路由规则,离开组件时被调用 */
1070+
beforeRouteLeave (to, from, next) {
1071+
// ...
1072+
}
1073+
```
1074+
1075+
7241076
Vue 中使用 TypeScript
7251077
---
7261078

0 commit comments

Comments
 (0)