mappState方法:用于方便我们映射state中的数据为计算属性
computed:{
...mapState(test:'test',demo:'demo')//对象写法
...mapState(['test','demo'])//数组写法
}
mapGetters方法:用于方便我们映射getters中的数据为计算属性
computed:{
...mapGetters(test:'test',demo:'demo')//对象写法
...mapGetters(['test','demo'])//数组写法
}
mapActions方法:用于方便我们生成actions对话的方法,即$store.dispatch(xxx)函数
methods:{
...mapActions(test:'test',demo:'demo')//对象写法
...mapActions(['test','demo'])//数组写法
}
mapMutations方法:用于方便我们生成mapMutations对话的方法,即$store.commit(xxx)函数
methods:{
...mapMutations(test:'test',demo:'demo')//对象写法
...mapMutations(['test','demo'])//数组写法
}
js文件模块化+命名空间示例
const test1 = {
namespaced:true,//开启命名空间
state:{test:2},
actions:{...},
mutations:{...},
getters:{test(state){...}}
}
const test2 = {
namespaced:true,//开启命名空间
state:{x:1},
actions:{...},
mutations:{...},
getters:{test(state){...}}
}
const store = new Vuex.Store({
modules:{
test1,test2
}
})
...mapState('test1',['test']),//读取state数据
...mapGetters('test1',['test']),//读取Getters数据
...mapMutations('test1',{...}),//读取Mutations数据
...mapActions('test1',{...})//读取Actions数据
在vue.config.js中配置代理
devServer: {
proxy: 'http://localhost:5000',
webSocketServer: false
}
devServer: {
proxy: {
'/api1':{ //匹配所有以'/api1'开头的请求路径
target: 'http://localhost:5000', //代理目标服务器的基础路径
changeOrigin:true, //默认为true,true:服务器收到的请求头中host为 localhost:5000;false:服务器收到的请求头中host为 localhost:8080
pathRewrite:{'^/api1':''} //替换匹配为空,避免后端服务无法识别请求真实路径
},
'/api2':{
target: 'http://localhost:5001',
changeOrigin:true,
pathRewrite:{'^/api2':''}
}
}
}
Slot分类
默认Slot
父组件
<Category>
<div>html结构1</div>
</Category>
子组件
<template>
<div>
<slot>插槽默认内容</slot>
</div>
</template>
父组件
<Category>
<template slot="demo1">
<div>html结构1</div>
</template>
<template v-slot:demo2>
<div>html结构2</div>
</template>
</Category>
子组件
<template>
<div>
<slot name='demo1'>插槽默认内容1</slot>
<slot name='demo2'>插槽默认内容2</slot>
</div>
</template>
const router = ({
routers:[
{
path: '/test1',
component:Test1,
//多级路由:使用children配置子级路由
children:[
{
name:'hello',//给路由命名,简化路由的跳转写法
path:'test1Child1', //此处不要写成'/test1Child1',默认会加'/'
component:Test1-1,
//接收参数
query:{
id:id,
name:name
}
},
{
path: 'test1Child2',
component:Test1-2
}
]
},
{
path: '/test2',
component:Test2
},
]
})
export default router
实现切换
...
props(route){
return {
id:route.query.id,
name:route.query.name
}
}
...
this.$router.push({//push模式
name:'test1',
params:{
id:xxx,
name:xxx
}
})
this.$router.replace({//replace模式
name:'test1',
params:{
id:xxx,
name:xxx
}
})
this.$router.forward()//前进
this.$router.back()//后退
this.$router.go()//前进
<keep alive include='test1'>
<router-view></router-view>
</keep-alive>
router.beforeEach((to,from,next)=>{...})//全局前置守卫,初始化时执行,每次路由切换前执行
router.afterEach((to,from,next)=>{...})//全局后置守卫,初始化时执行,每次路由切换后执行
beforeEnter(to,from,next){...})//独享守卫
beforeRouteEnter(to,from,next){...})//进入组件守卫,通过路由规则,进入该组件时被调用
beforeRouteLeave(to,from,next){...})//离开组件守卫,通过路由规则,离开该组件时被调用