vue路由跳转及其ajax请求,Vue中的路由以及ajax

Vue请求ajax之fetch

使用方法:

fetch(url,{

headers:{

"token":localStorage.getItem('token'),

"content-type":"apllication-xxx-urlencoded"

},

methods:"GET/POST",

data:{

}

}).then(function(res){

return res.json()  //text() arrayBuffer() blob() formData()

}).then(res=>{

//res 才是请求的结果 格式化之后(什么格式取决于 第一个 then 返回的处理方法)

})

fetch 兼容性差:使用promise 不支持 ie11及以下的 且,在vue react等脚手架中 配置babel es6转es5,也无法转换promise

使用babel-polyfill 插件来解决

Vue 如何兼容IE(IE9及以上),  使用babel-polyfill 插件来解决

解决fetch 兼容性问题?

1,使用babel-polyfill

2,直接使用 git https://github.com/github/fetch

基于原生的fetch 封装的,当有的浏览器不支持fetch时,转换成普通的xhr对象(内部集成了babel-polyfill)

Vue的axiso ajax库

axios 是一个 封装好的 第三方ajax请求的库

特点:支持restfulapi    支持promise 解决回到地狱    支持ajax请求拦截

get请求:

axios.get('url?params1=v&params2=值2').then().catch()

axios.get(url,{

params:{

key1:v1,

key2:v2

}

})

post请求:

axios.post(url,{

key1:v1,

key2:v2

}).then().catch()

Vue中的路由

单页面应用:single page application     整个项目只有一个html

优点:

页面性能较高

页面间切换速度很快

缺点:

不利于seo (搜索引擎优化)

爬虫 爬取页面关键字

单页面应用一般都是基于webpack进行构建的,所以资源,都是在js中,爬虫不认识js

vue路由:  一个地址 对应 一个组件

Vue中的嵌套路由

1,在以及路由 路由规则中 routes 中 定义 children属性([]);在children属性中,定义该路由的子路由 规则(子路由路径 最好加上 父路由路径作为前缀)

2,在 一级路由 对应的 组件  中 设置 router-view 作为二级路由的出口

3,设置 一级路由中 默认显示某个二级路由

{

path:"/news",

redirect:"/news/nativeNews"

},

{

path:"",

component:NativeNews

},

Vue中的命名路由

定义路由规则时,可以给当前 路由起一个名字  (定义一个name属性)

const routes = [

{

path:"/",

redirect:"/home"

},

{

path:"/home",

name:"home",

component:Home

},

{

path:"/news",

name:"news",

component:News

}

];

router-link

to属性值

字符串

router-link to="/news" 通过path跳转

对象

router-link :to="{name:'home'}" 可以通过名字跳转

router-link :to="{path:'/home'}" 可以通过path跳转

传参  params传参

router-link :to="{name:'home',params:{id:111}}"

在目标路由中  this.$route.params.id获取

注意:

1,params传参 是 隐式(不会在地址栏显示),刷新后 参数会丢失

2,params传参 必须 和 name跳转路由 一起使用,和path 不起效果

Vue编程式导航

声明式导航 : 在模板(html中),通过标签(组件) 跳转  router-link跳转

编程式导航 : 在js中通过api跳转

vueRouter 给 Vue的原型中 还灌入一个对象  $router,保存了 操作 路由的 api

push

this.$router.push()  跳转路由

push参数 同 router-link to属性的值(一毛一样)

字符串

this.$router.push("/news") 通过path跳转

对象

this.$router.push({name:'home'}) 可以通过名字跳转

this.$router.push({path:'/home'}) 可以通过path跳转

传参  params传参

this.$router.push({name:'home',params:{id:111}})

在目标路由中  this.$route.params.id获取

this.$router.replace() 参数 同上

与push不同的是?

跟 router.push 很像,唯一的不同就是,它不会向 history 添加新记录,而是跟它的方法名一样 —— 替换掉当前的 history 记录。  (返回上一步 时,之前 不存在了,到上两步)

this.$router.go(n)

go(0)  刷新

go(1) 前进一步

go(-1) 返回上一步

Vue中query传参

router-link

:to="{path:'/news',query:{id:1,id2:2}}"

this.$router.push({

path:'/news',

query:{

id:1,

id2:2

}

})

获取: this.$route.query.xxx

Vue中的导航守卫

全局守卫

全局前置守卫

router.beforeEach((to,from,next)=>{

});

全局后置守卫

router.afterEach((to,from)=>{

})

路由独享

只针对某个路由

routes:[

{

path:"/home",

component:Home,

beforeEnter:(to,from,next)=>{

}

}

]

组件内部

beforeRouteEnter

beforeRouteUpdate  新增 主要用于 动态路由

beforeRouteLeave

标签:Vue,home,ajax,跳转,router,path,路由

来源: https://www.cnblogs.com/broue/p/13423434.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个典型的前后端分离的例子,我可以给您提供一个简单的示例代码,来帮助您理解如何使用 Vue3 + Vite + Ajax + SpringBoot 实现增删改查功能。 前端使用 Vue3 + Vite + Ajax 实现: 1. 在 Vue3 ,使用 `axios` 或者 `fetch` 进行 AJAX 请求请求后端 SpringBoot 服务器,获取数据或者提交数据。 2. 在 Vue3 ,使用 `vue-router` 进行 SPA 单页应用的路由控制,实现前端页面的跳转。 3. 在 Vue3 ,使用 `vuex` 进行状态管理,方便组件之间的数据共享和传递。 下面是一个简单的 Vue3 组件的示例代码: ```vue <template> <div> <h1>用户列表</h1> <table> <thead> <tr> <th>ID</th> <th>用户名</th> <th>邮箱</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="user in users" :key="user.id"> <td>{{ user.id }}</td> <td>{{ user.username }}</td> <td>{{ user.email }}</td> <td> <button @click="editUser(user)">编辑</button> <button @click="deleteUser(user.id)">删除</button> </td> </tr> </tbody> </table> </div> </template> <script> import axios from 'axios'; export default { name: 'UserList', data() { return { users: [], }; }, methods: { async getUsers() { const response = await axios.get('/api/users'); this.users = response.data; }, async deleteUser(id) { await axios.delete(`/api/users/${id}`); this.getUsers(); }, editUser(user) { // 跳转到编辑用户页面,使用 vue-router this.$router.push(`/users/${user.id}`); }, }, mounted() { this.getUsers(); }, }; </script> ``` 后端使用 SpringBoot 实现: 1. 在 SpringBoot ,使用 `@RestController` 和 `@RequestMapping` 注解定义 RESTful API 接口,实现增删改查的逻辑。 2. 在 SpringBoot ,使用 `@Service` 和 `@Repository` 注解定义服务层和数据访问层,实现业务逻辑和数据库操作。 3. 在 SpringBoot ,使用 `Spring Data JPA` 或者 `MyBatis` 等框架进行数据库操作,简化代码编写。 下面是一个简单的 SpringBoot Controller 的示例代码: ```java @RestController @RequestMapping("/api/users") public class UserController { private final UserService userService; public UserController(UserService userService) { this.userService = userService; } @GetMapping public List<User> listUsers() { return userService.listUsers(); } @PostMapping public void createUser(@RequestBody User user) { userService.createUser(user); } @PutMapping("/{id}") public void updateUser(@PathVariable Long id, @RequestBody User user) { userService.updateUser(id, user); } @DeleteMapping("/{id}") public void deleteUser(@PathVariable Long id) { userService.deleteUser(id); } } ``` 这是一个简单的增删改查的示例代码,您可以根据自己的需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值