狂神 Vue 钩子函数与异步请求

news/2024/7/10 0:55:16 标签: vue, ajax, javascript, vue.js, node.js

404和路由钩子

参数传递

将Login界面的用户名显示到main页面

先从Login界面,转发时加上用户名输入框的数据

vue"><el-form-item label="账号" prop="username">
  <el-input type="text" placeholder="请输入账号" v-model="form.username"/>
</el-form-item>
this.$refs[formName].validate((valid)=>{
          if(valid){
            /*如果引用成功了,会去router中push到main,这种方式称为编程式导航*/
            this.$router.push("/main/"+this.form.username);

          }

这样push过去的就是/main/username,账号的数据

index.js

export default new Router({
  routes: [
    {
      path: '/main/:name',
      component: Main,
      props:true,

/main的时候会传递name参数

在Main.vue接收就行

vue"><script>
  export default {
    props:['name'],
vue"><el-header style="text-align: right; font-size: 12px">
  <el-dropdown>
    <i class="el-icon-setting" style="margin-right: 15px"></i>
    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item>个人信息</el-dropdown-item>
      <el-dropdown-item>退出登录</el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
  <span>{{name}}</span>
</el-header>

{{name}}接收

image-20200908102038236

image-20200908102055445

通过url传递过来账户的数据

路由模式

两种路由模式

hash :路径带#号

history:路径不带#号

只要设置一个模式就行

index.js

export default new Router({
  mode:"history",

设置一个模式,写法注意时mode,history要加双引号

image-20200908102547931

显示的url就不带#号

跳转404

要安装axios、webpack、router、elementui

404就是找不到而已,也写一个组件,路由绑定一下就行,跟放的顺序没什么关系

,{
    path: "*",
    component: NotFound
  }
]

image-20200908103111476

钩子函数与异步请求

类似于过滤器,通过加方法能传递数据

Profile.vue

vue"><template>
  <div>
    <h1>个人信息</h1>
    {{id}}
  </div>
</template>

<script>
    export default {
        props:['id'],
        name: "UserProfile",
      beforeRouteEnter:(to,from,next)=>{
        console.log("进入路由之前");
        next(vm => {
          vm.getData();
        });
      },
      beforeRouteLeave:(to,from,next)=>{
        console.log("进入路由之后");
        next();

      },
      methods:{
        getData:function () {
          this.axios({
            method:"get",
            url:"http://localhost:8080/static/mock/data.json"
          }).then(function (response) {
            console.log(response)
          })
        }
      }

    }

</script>
<style scoped>
</style>

index.js

import Vue from 'vue'
import Router from 'vue-router'
import Main from "../views/Main";
import Login from "../views/Login";

import UserList from "../Views/user/List"
import UserProfile from "../Views/user/Profile"
import NotFound from "../NotFound";
import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(VueAxios, axios)

Vue.use(Router)

export default new Router({
  mode:"history",

  routes: [
    {
      path: "*",
      component: NotFound
    },
    {
      path: '/main',
      component: Main,
      props:true,
      children:[
        {
          path:"/user/profile/:id",
          name:"UserProfile",
          component:UserProfile,
          props:true
        },{
          path:"/user/list",
          component:UserList
        },{
          path: "/goHome",
          redirect:"/main"
        }

      ]
    },{
      path: '/login',
      component: Login
    }
  ]
})

加入axios,加入方法可以去官网看

image-20200908221404049

image-20200908221852453

将json的数据打印到了cosole


http://www.niftyadmin.cn/n/1699132.html

相关文章

【差分+二分答案】【NOIP2012】借教室

Description 在大学期间&#xff0c;经常需要租借教室。大到院系举办活动&#xff0c;小到学习小组自习讨论&#xff0c;都需要向学校申请借教室。教室的大小功能不同&#xff0c;借教室人的身份不同&#xff0c;借教室的手续也不一样。 面对海量租借教室的信息&#xff0c;我们…

SpringBoot MartinFlowar微服务译文理解

微服务 是一种架构风格&#xff0c;将业务拆分成模块&#xff0c;部署在不同的主机上提供结构&#xff0c;提供服务&#xff0c;通过http的方式通信 单体应用架构&#xff08;All in One&#xff09; 将一个应用的所有应用服务都封在一个应用中&#xff0c;无论什么系统&…

4、自定义菜单

1、自定义菜单最多包括3个一级菜单&#xff0c;每个一级菜单最多包含5个二级菜单。 2、一级菜单最多4个汉字&#xff0c;二级菜单最多7个汉字&#xff0c;多出来的部分将会以“...”代替。 3、创建自定义菜单后&#xff0c;菜单的刷新策略是&#xff0c;在用户进入公众号会话页…

mysql数据库 zip压缩效率_MySQL数据库中压缩表性能监测-爱可生

由于压缩表一般都是对字符串类的数据&#xff0c;比如 TEXT,VARCHAR 等压缩&#xff0c;所以针对这块的数据做更新&#xff0c;很容易就把更改日志(上篇介绍过)打满&#xff0c;继而造成频繁的解压和压缩操作。总的来说压缩表适合于读密集、只读、或者极少量更新的业务场景。二…

狂神 SpringBoot 自动装配原理初探

原理初探 自动配置 pom.xml spring pom.xml下有parent spring-boot-starter-parent下还有parent spring-boot-dependencies没有parent了&#xff0c;里面存放着各种东西的jar包 所以pom.xml中 spring-boot-dependencies &#xff1a;存放着核心依赖我们在写或者引入一些sp…

jquery在线引用

转载&#xff1a;http://www.cnblogs.com/lzx-1024/p/7716615.html jquery-3.1.1&#xff08;最新&#xff09;官网jquery压缩版引用地址:<script src"https://code.jquery.com/jquery-3.1.1.min.js"></script> jquery-3.0.0官网jquery压缩版引用地址:&l…

IBatis的分页研究

IBatis的分页研究 博客分类&#xff1a; Ibatis学习摘自&#xff1a; http://cpu.iteye.com/blog/311395 yangtingkun Oracle分页查询语句 ibaits.jar OracleDialect.java 在看JPetStore的代码时&#xff0c;发现它的分页处理主要是通过返回PaginatedList对象来完成的。如&am…

第一个SpringBoot程序 修改banner

第一个SpringBoot程序 两种创建方法 使用网站生成项目 https://start.spring.io/ maven、java版本、项目名称、导出jar、添加springWeb 这种方式一般不用&#xff0c;一般直接idea生成 IDEA创建 可能会遇到的问题 无效发行版本 项目结构设置 项目打开&#xff0c…