• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Vue项目中最新用到的一些实用小技巧

vue 搞代码 4年前 (2022-01-08) 27次浏览 已收录 0个评论

这篇文章主要给大家介绍了关于Vue项目中最新用到的一些实用小技巧,文中通过示例代码介绍的非常详细,对大家学习或者使用vue具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

写在前面

在最近的 Vue 项目中,为了完成需求使用了一些小技巧,做个笔记,或许也能帮到道友。

阅读重点

需求一:为路径配置别名

在开发过程中,我们经常需要引入各种文件,如图片、CSS、JS等,为了避免写很长的相对路径(../),我们可以为不同的目录配置一个别名。

找到 webpack.base.config.js 中的 resolve 配置项,在其 alias 中增加别名,如下:

创建一个 CSS 文件,随便写点样式:

 .avatar display: flex; justify-content: center; align-items: center; .avatar-img padding 20px border solid 1px #ccc border-radius 5px

接着,在我们需要引入的文件中就可以直接使用了:

  <div class="avatar"> </div> @import "~css/avatar"; 

需要注意的是,如果不是通过 import 引入则需要在别名前加上 ~,效果如下:

需求二:要求实现在生产包中直接修改api地址

这个需求,怎么说呢,反正就是需求,就想办法实现吧。

假设有一个 apiConfig.js 文件,用于对 axios 做一些配置,如下:

 import axios from 'axios'; axios.defaults.timeout = 10000; axios.defaults.retry = 3; axios.defaults.retryDelay = 2000; axios.defaults.responseType = 'json'; axios.defaults.withCredentials = true; axios.defaults.headers.post["Content-type"] = "application/json"; // Add a request interceptor 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); }); // Add a response interceptor axios.interceptors.response.use(function (response) { // Do something with response data return response; }, function (error) { // Do something with response error return Promise.reject(error); }); export default axios

在 static 文件夹中增加一个 config.json 文件,用于统一管理所有的 api 地址:

 { "base":<span style="color:transparent">本文来源gaodai#ma#com搞*!代#%^码$网!</span> "/api", "static": "//static.com/api", "news": "//news.com.api" }

打开 main.js,写入下列代码:

 // The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' import router from './router' import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; import axios from 'js/apiConfig'; //import直接引入,不用添加~ Vue.config.productionTip = false; Vue.use(ElementUI); /* eslint-disable no-new */ let startApp = function () { let randomStamp = new Date().getTime(); axios.get(`/static/config.json?t=${randomStamp}`).then((data) => { axios.defaults.baseURL = data.base; //设置一个默认的根路径 Vue.prototype.$axios = axios; Vue.prototype.$apiURL = data; //将所有路径配置挂载到 Vue 原型上 /* eslint-disable no-new */ new Vue({ el: '#app', router, components: {App}, template: '' }); }) }; startApp();

就是先用 axios 获取 api 文件,然后再初始化。

需求三:由后台根据用户权限值返回菜单

菜单是树形结构(PS:就算不是树形结构,你也得处理成树形结构),我这里使用的是 ElementUI ,参考了道友的这篇文章,实现如下:

新建一个 Menu.vue 文件,写入如下代码:

  .el-menu-vertical-demo:not(.el-menu--collapse) { width: 200px; min-height: 400px; } 

这里主要用到两个东西,一个是 render 函数,一个是递归,如果不熟悉 render 函数的道友请点这里。可能有道友会问为什么不用模板,因为・・・・・・做不到啊

以上就是Vue项目中最新用到的一些实用小技巧的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Vue项目中最新用到的一些实用小技巧

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址