Vue 开发流程

1:添加路由规则
路由规则在router/index.js 中编写;在入口文件main.js中导入router文件,当我们需要为项目编写路由规则时,就需要在router/index.js 中编写。

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

/**
 * constantRoutes
 * 公共路由 默认路由 
 * 
 */
export const constantRoutes = [

  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  }
]


const createRouter = () => new Router({
  mode: 'history', // 默认路由模式  hash或者history模式
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})
const router = createRouter()
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher 
}
export default router


2:添加要跳转的路径
在添加路由规则时,通过component属性可以设置要跳转的路径,并且需要在对应的路径上创建对应的文件。
如 :component: () => import('@/views/error-page/404')(注:404是.vue文件)

3:定义接口路径
在页面中引入api下的js文件,该文件中定义了接口路径的一部分。
接口路径的另一部分是 config/dev.env.js 中的BASE_API属性,BASE_API与url共同构成了项目页面的访问路径。

import request from '@/utils/request'

export function selBzchByFile(query) {
  return request({
    url: '/user/userinfo',
    method: 'post',
    data: query
  })
}


4:使用axios进行接口调用
使用axios进行接口调用,把接口返回数据在页面
axios({
  method: 'post',
  url: 'http://test.cn/pel/expInfo', // 请求地址
  data: this.listQuery, // 参数
  responseType: 'blob', // 表明返回服务器返回的数据类型
  headers: {
    'token-auth' : getToken(),
    'Content-Type': 'application/json'
  }
}).then(res => {
  const link = document.createElement("a");
  let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
  link.style.display = "none";
  link.href = URL.createObjectURL(blob);
  link.setAttribute("download", decodeURI(res.headers['filename']));
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  setTimeout(() => {
  this.listLoading = false
}, 1.5 * 1000)
}).catch(err => {
  console.log(err)
});

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值