vue stylus 安装
$ npm install stylus --save
$ npm install stylus-loader --save
Stylus是一个CSS预处理器。那么在SaaS,Less和Stylus中,为什么选择后者呢?
因为Stylus是来源于Node.js社区,与js关系密切,所以基于Vue.js的开发,我们选择使用Stylus。
[详情查看](https://stylus.bootcss.com/)
vue 全局引入外部css文件
main.js 使用 import 导入外部css文件
import './assets/styles/reset.css'
vue css使用
组件内部css中引入其他css时,import需要加上@,路径需要加上~,
- @import '~../../../assest/styles/reset.css'
css样式穿透, scoped只修饰当前组件,使用插件渲染的组件需要使用样式穿透 >>>
vue iconfont使用
import '@/assets/styles/iconfont.css'
<i class="iconfont iconhanhui"></i>
vue 路径优化之resolve的使用
import CommonGallery from '../../../common/gallery/Gallery'
@import '~../../../assets/styles/variables.styl'
这时配置build/webpack.bas.conf.js,resolve配置项alias属性
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src'),
'$styles': resolve('src/assets/styles'),
'$common': resolve('src/common')
}
},
配置后就可以使用“别名”来引用相关组件
import CommonComponent from '$common/gallery/Gallery'
@import '~$styles/variables.styl'
vue 移动端点击300毫秒延迟
fastclick常用于消除移动端点击事件触发的300ms延迟
$ npm install fastclick --save
import fastClickAttach from 'fastclick'
fastClickAttach.attach(document.body)
vue 使用 vue-awesome-swiper
import Vue from 'vue'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
Vue.use(VueAwesomeSwiper, )
<template>
<swiper :options="swiperOptions">
<swiper-slide>
<img />
.....
</swiper-slide>
<div class="swiper-pagination" slot="pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
.....
</swiper>
</template>
<script>
export default {
data () {
return {
swiperOptions: {
initialSlide: 2,
direction : 'vertical',
speed: 300,
loop: true,
observer: true
observeParents: true,
autoplay: true,
autoplay: {
delay: 1000,
},
pagination: {
el: '.swiper-pagination',
type: 'bullets',
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
}
}
}
}
</script>
vue 移动端+swiper多页图标渲染
一些app都有可以滑动的图标区域,假如一页只能显示8个图标,现在获取到数据一共有10个或多条数据
怎么渲染出根据数据来渲染几页的图标呢,核心思想就是一个二维数组
cosnt iconArr = ['icon1', 'icon2', 'icon3', 'icon4', 'icon5', 'icon6', 'icon7', 'icon8', 'icon9', 'icon10']
let pages = []
iconArr.forEach((item, index) => {
const page = Math.floor(index / 8)
if (!pages[page]) pages[page] = []
pages[page].push(item)
})
return pages
<template>
<swiper :options="swiperOptions" v-for="(page, index) of pages">
<swiper-slide v-for="(item, index) of page">
<span class="swiper-icon">{{ item }}</span>
</swiper-slide>
<div class="swiper-pagination" slot="pagination"></div>.
</swiper>
</template>
vue 路由模式
vue-router 默认hash模式——使用URL的hash来模拟一个完整的URL,于是当URL改变时,页面不会重新加载。
hash模式会自动在url地址末尾加上
如果不想要很丑的hash,我们可以用路由的history模式,这种模式充分利用history.pushStateAPI来完成URL跳转而无须重新加载页面。
history模式打包问题,........
vue 路由配置 scrollBehavior, 只有history模式下才有用
使用前端路由,当切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像重新加载页面那样。
当创建一个 Router 实例,你可以提供一个 scrollBehavior 方法:
scrollBehavior (to, from, savedPosition) {
return { x: 0, y: 0 }
}
vue 函数节流 提高性能
防抖和节流的目的都是为了减少不必要的计算,不浪费资源,只在适合的时候再进行触发计算。
1. 函数防抖
- 在事件被触发n秒后再执行回调,如果在这n秒内又被触发,则重新计时;
典型的案例就是输入搜索:输入结束后n秒才进行搜索请求,n秒内又输入的内容,就重新计时。
- 实现原理, 基本思想就是设置一个定时器,在指定时间内执行代码清楚上一次的计时器,并设置另一个定时器,直到函数请求停止并超过时间间隔
- 使用场景, 文本框输入搜索(连续输入避免多次请求接口)
- 大致代码实现:
export default {
dadta() {
return {
timer: ''
}
},
watch: {
inputValue () {
if (this.timer) clearTimeout(this.timer)
this.setTimeout(()=> {
axios.get('/getData', { params:{ inputValue: this.inputValue } })
.......
}, 100)
}
}
}
2. 函数节流
- 规定在一个单位时间内,只能触发一次函数,如果这个单位时间内触发多次函数,只有一次生效;
典型的案例就是鼠标不断点击触发,规定在n秒内多次点击只有一次生效
- 实现原理, 用时间戳来判断是否到了回调执行,记录上次的时间戳,每次mousemove时间执行回调,
回调中判断当前时间戳到上次时间戳是否达到在规定时间段,如果是,执行回调函数,并更新时间戳
- 使用场景, resize,scroll,mousemove,touchmove,
- 代码大致实现:
export default {
data () {
return {
timer: '',
lastTime: ''
}
},
methods: {
handleMousemove () {
const nowTime = Date.now()
const delay = 200
if (this.lastTime && (nowTime - this.lastTime) <= delay) {
if(this.timer) clearTimeout(this.timer)
this.timer = setTimeout(() => {
this.lastTime = nowTime
this.handleMove()
}, delay)
}else {
this.lastTime = nowTime
this.handleMove()
}
},
handleMove () {
.......
}
}
}
- better-scroll是解决移动端滚动的一种方案,核心是基于iscroll实现,还扩展了某些功能
- better-scroll是通过纯javascript实现的,意味着没有依赖项,是一个轻量级的javascript库
- install
$ npm install better-scroll --save
- template结构,better-scroll应用于外部的warpper容器,滚动部分是content
<template>
<div class="wrapper" ref="wrapper">
<ul class="content">
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
// 放置一些其他内容,不会影响滚动
</div>
</template>
- vue 组件中使用
import BScroll from 'better-scroll'
mounted () {
this.scroll = new BScroll(this.refs.wrapper, { click: true })
}
- 滚动的原理
.wrapper容器,设置其高度固定,.content是父容器的第一个子元素,其高度随其内容的大小而增加
当内容的高度不超过父容器的高度时,内容将不会滚动,一旦超过,就可以滚动内容
当手指在右边字母表中移动时,页面内容滑动到相对应的字母开头的城市
better-scroll提供一个原型上的方法: this.scroll.scrollToElement(el,time,offsetX,offsetY,easing)
el: 目标DOM元素。如果值是一个字符串,底层使用queryelector获取元素
time: 滚动到对应元素需要的动画时间, 单位毫秒
offsetX: x到目标元素的偏移量,如果值为true,则滚动到目标元素的中心。
offsetY: y到目标元素的偏移量,如果值为true,则滚动到目标元素的中心。
easing: 动画过渡的速度,linear,slow.....
this.scroll.scrollToElement(this.refs['A'])
[更多配置信息查看](https:
vue touchEvent事件
touchstart
touchmove
touchend
touchEvent.touches事件是一个touchList集合,使用下标[0] 可以获取手指到浏览器距离等信息
vue 列表渲染ref绑定返回一个数组
当列表渲染组件时绑定ref时,ref的时候返回的是一个数组
const data = ['one', 'two', 'three']
<ul>
<li v-for="item in data" :ref="item">item</li>
</ul>
this.$refs.one
vue 本地存储sessionStorage和localStorage
对浏览器来说,使用 Web Storage 存储键值对比存储 Cookie 方式更直观,而且容量更大,
它包含两种:localStorage 和 sessionStorage
- sessionStorage(临时存储) :为每一个数据源维持一个存储区域,在浏览器打开期间存在,包括页面重新加载
- localStorage(长期存储) :与 sessionStorage 一样,但是浏览器关闭后,数据依然会一直存在
sessionStorage 和 localStorage 的用法基本一致,引用类型的值要转换成JSON
const info = { name: 'hou', age: 24, id: '001' }
const str ='hehe'
sessionStorage.say = str
localStorage.setItem('say', JSON.stringify(info))
sessionStorage.say
localStorage.getItem('say')
sessionStorage.removeItem('say')
localStorage.clear()
- localStorage有效期是永久的。一般的浏览器能存储的是5MB左右。sessionStorage api与localStorage相同。
- sessionStorage默认的有效期是浏览器的会话时间(也就是说标签页关闭后就消失了)。
- localStorage作用域是协议、主机名、端口。(理论上,不人为的删除,一直存在设备中)
- sessionStorage作用域是窗口、协议、主机名、端口。
- localStorage是window上的。所以不需要写this.localStorage,vue中如果写this,是指vue实例。会报错
vue keep-alive 组件
<keep-alive> 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们。主要用于保留组件状态或避免重新渲染。
是一个抽象组件:它自身不会渲染一个 DOM 元素,也不会出现在组件的父组件链中
当组件在 <keep-alive> 内被切换,它的 activated 和 deactivated 这两个生命周期钩子函数将会被对应执行。
- include - 字符串或正则表达式。只有名称匹配的组件会被缓存。
- exclude - 字符串或正则表达式。只有名称匹配的组件都不会被缓存。
- max - 数字。最多可以缓存多少组件实例。
<keep-alive include="a, b"></keep-alive>
<keep-alive include="/a|b/"></keep-alive>
<keep-alive include="['a', 'b']"></keep-alive>
vue 递归组件
组件是可以在它们自己的模板中调用自身的。不过它们只能通过 name 选项来做这件事
实现一个城市多级选择组件,加入有以下城市数据
const data = [{
"code": "500000",
"name": "重庆市",
"list": [{
"code": "500000",
"name": "重庆市",
"list": [{
"code": "500101",
"name": "沙坪坝区",
"list": [{
"code": "....",
"name": "西永"
}, {
"code": "....",
"name": "大学城"
}]
},{
"code": "500102",
"name": "九龙坡区"
}]
}]
}]
<template>
<div class="wrapper">
<div class="city" v-for="(item, index) of cities" :key="index">
<div class="cityName">{{ item.name }}</div>
<div v-if="item.list">
<city :cities="item.list"></city>
</div>
</div>
</div>
</template>
<sciprt>
export default {
name: 'city',
props: {
cities: Array
},
}
</sciprt>
vue 组件中export default的name属性作用
1. 在keep-alive组件中匹配
2. 递归组件时可以调用自身
3. 在页面使用dev-tools工具可以查看组件的名字就是name属性的值
vue 手机调试
babel-polyfill包,手机白屏(是手机不支持promise)
在手机端浏览, package.json/script/dev后面加上
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js --host 0.0.0.0",
打包上线
$ npm run build
如果希望打包生成的文件最后在后端一个project目录运行代码
assetsPublicPath: '/project'
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!