vue路由,状态管理,网络请求

目录

一、vue-router基本使用

二、vue-router参数传递

三、vue-router导航守卫

四、状态管理

五、网络请求


Home | Vue Router (vuejs.org) 路由网站

Vue:在不同的vue之间跳转就使用路由,路由就是一个vue地址

整个的文件目录

 

一、vue-router基本使用

1、安装vue-router:npm install vue-router@4

2、创建路由,在src下创建新文件夹router,再建文件index.js。src下新建views,其中包括about和home,在里面都新建index.vue

27eb49e5ad0d43e48f7764adadd0c392.png

import { createRouter, createWebHashHistory } from 'vue-router'

const routes=[{
    path:'/home',
    name:'home',
    component:()=>import('../views/home/index.vue')
},
{
    path:'/about',
    name:'about',
    component:()=>import('../views/about/index.vue')
}]

const router = createRouter({
    routes,
    history:createWebHashHistory()
})

export default router 

0e7602a3da19429fbb3304cb62673dbd.png

 

<script>

</script>

<template>
    <div class="app-container">
        about
    </div>
</template>

<style scoped>

</style>

​​​​​​​9c2a42b67d10449ca086f0f2e08a4f85.png

<script>
export default{
    methods:{
        gotoAbout(){
            this.$router.push({path:'/about'})
        }
    }
}
</script>

<template>
    <div class="app-container">
        home
        <button @click="gotoAbout()">跳转到about</button>
    </div>   
</template>

<style scoped>
</style>

3、修改main.js中的内容,导入(import)路由并且使用

7b1ac9bce9a343739cf2b485800cd493.png

import { createApp } from "vue"
import parent from './parent.vue'
import router from "./router"

const app =createApp(parent)
app.use(router)
app.mount("#app")

4、在父vue中显示出来

6d34bba179384b63a5a7b201661d5c94.png

<script>
</script>

<template>
<router-view></router-view>
</template>

<style scoped>
</style>

二、vue-router参数传递

1、使用路由传递数据,要先修改路由index.js

3c7e5ed4538c49f8a708231343d430e0.png

import { createRouter, createWebHashHistory } from 'vue-router'

const routes=[{
    path:'/home',
    name:'home',
    component:()=>import('../views/home/index.vue')
},
{
    path:'/about',
    name:'about',
    component:()=>import('../views/about/index.vue')
},
{
    path:'/about/:id',
    component:()=>import('../views/about/index.vue')
}]

const router = createRouter({
    routes,
    history:createWebHashHistory()
})

export default router 

2、在home/index.vue中,修改跳转路径

64c101677e7443998d6ccea6bfd039a5.png

<script>

export default{
    methods:{
        gotoAbout(){
            this.$router.push({path:'/about/12'})
        }
    }
}
</script>
<template>
    <div class="app-container">
        home
        <button @click="gotoAbout()">跳转到about</button>
    </div>   
</template>


<style scoped>
</style>

3、在about/index.vue中

1f78c006864a4c82913fffbb58bf0ea6.png

<script>
export default{
    data(){
        return{
            id:0
        }
    },
    created(){
        if(this.$route.params && this.$route.params.id){
            this.id=this.$route.params.id
        }
    }
}
</script>

<template>
    <div class="app-container">
        about<br>
        {{id}}
    </div>
</template>

<style scoped>

</style>

 4、结果

896417de57a14a499b6c3b255d86708e.png

 0eb036b8aab84cda92b649e22003d9ee.png

三、vue-router导航守卫

例:现在有些网页登陆之后才能访问下一个页面,导航守卫就是这个功能。

  1. 在路由的index.js中加入

aa9f9eb906cd4d29980ed94f698e81b0.png

router.beforeEach((to,from) => {
    // ...
    //返回false以取消导航
    return true
})

fb4700d1423948b7b2c8827881696d66.png

2、当把false改为true时,页面就会显示出来

e9065201d3f24fedbbd30bfa60945b9d.png

四、状态管理

State:全局状态管理,可以在vue之间共享数据。

1、在src中创建store文件夹,并且在里面建立index.js,再在main.js中全局注册

7851192b999c42aa92d9d194fe64024d.png

import { createStore } from 'vuex'

// 创建一个新的 store 实例
const store = createStore({
    //存放数据
  state () {
    return {
      count: 10
    }
  },
  //mutations里面存放操作state中数据的方法
  mutations: {
    increment (state) { //增加
        state.count++
    },
    substract (state) { //减少
        state.count--
  },
    setCount (state,payload) { //重新赋值
        state.count=payload
}
}
})
export default store

f64626cf99ca45d48ca1a7b433ff4279.png

//main.js
import { createApp } from "vue"
import parent from './parent.vue'
import router from "./router"
import store from "./store"

const app =createApp(parent)
app.use(router)
app.use(store)
app.mount("#app")

  

2、在全局中显示,并且可以调用index.js命令,对原始数据进行修改。

0718505d54ed4ae6a5e956c096de7c07.png

<script>
import { useStore } from 'vuex'
const store=useStore()

export default{
    methods:{
        gotoAbout(){
            this.$router.push({path:'/about/12'})
        },
        updataCount(){ 
            this.$store.commit("setCount",100)
        }
    }
}
</script>
<template>
    <div class="app-container">
        home
        <hr>
        {{this.$store.state.count}}
        <hr>
        <button @click="gotoAbout()">跳转到about</button>
        <button @click="updataCount()">修改store中的count数据</button>
    </div>   
</template>


<style scoped>
</style>

2420d478d35c4b57b401081175374f4d.png

 2e6b19e7317440ca95ed9b8698aae6ab.png

3、在about/index.vue中写入该命令,也会显示,并且随重新赋值而变化

7b3d30fb4ad8415097ed817d43c25375.png

67c8d077e9b24835b7d82fa5a078aa1a.png

<script>
export default{
    data(){
        return{
            id:0
        }
    },
    created(){
        if(this.$route.params && this.$route.params.id){
            this.id=this.$route.params.id
        }
    }
}
</script>

<template>
    <div class="app-container">
        about<br>
        {{id}}<br>
        {{this.$store.state.count}}
    </div>
</template>

<style scoped>

</style>

五、网络请求

1、axios、get、post

安装:npm install axios

ea12abd5da454206adea4566b120cea8.png

import { axios } from "axios"

const request=axios.create({
    baseURL:'' //指定某一请求的域名
})

//请求拦截器,发送请求之前的拦截器
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

//响应拦截器,收到服务器端响应之后触发该拦截器
axios.interceptors.response.use(function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response;
  }, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  });
  export default request

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值