vue中 <img :src="item.img" alt="" />src的值是一个变量时存在的问题
如果src的值是一个变量,同时有使用了@/asset/img,则变量解析时会直接解析成字符串 @/asset/img,不会转成 …/…/set/img。( 00)
解决方法:使用new URL(url, base) 返回处理后的路径。
url:可以是一个绝对或相对的url。。如果 是相对 url, 则会将 base 用作基准 url。如果是绝对url,base会被忽略。
例如:
let baseUrl = "https://developer.mozilla.org";
let A = new URL("/a", baseUrl);
// => 'https://developer.mozilla.org/a'
import.meta.url 表示当前模块的 file: 绝对路径。例如:file:///Users/jingjingjing/Desktop/vite/vite-project2/1.ts
export const getImage = (image) => {
const url = new URL(`../assets/img/${image}`, import.meta.url);
console.log(url);
return new URL(`../assets/img/${image}`, import.meta.url).href;
};
url:

const list = [
{
url: "/",
img: "tabbar/home.png",
name: "首页",
},
{
url: "/favor",
img: "tabbar/favor.png",
name: "收藏",
},
{
url: "/order",
img: "tabbar/order.png",
name: "订单",
},
{
url: "/message",
img: "tabbar/message.png",
name: "消息",
},
];
<template v-for="(item, index) in list" :key="item.url">
<img :src="getImage(item.img)" alt="" />
</template>
react中可以使用 require(${imageUrl})来解决:
// 拼接路径别名和url变量
const imagePath = require(${imageUrl});
// 在标签中使用拼接后的路径
修改第三方库的样式(vant)
1.用插槽插入自己的样式,就可以在自己的作用域中修改
<style scoped lang="scss"><style >
2.定义全局变量
:root{
color:red ! !important;
}
3. 组件库会定义一些变量, 覆盖它的默认值
:root:root {
--van-color: rgb(0, 255, 68);
--van-search-left-icon-color: var(--van-color);
--van-calendar-popup-height: 100vh;
--van-button-primary-background: #fa7319;
}
- 找到对应的子组件的选择器修改,需要使用:deep()
:deep(.vn-tabbar-item_icon){
}
.tabber隐藏的两种方式
1.在路由中设置meta,通过meta判断是否隐藏
{
path: "/detail/:id",
component: Detail,
meta: {
hidden: true,
},
},
----------------------------------------------
const route = useRoute();
<Tabbar v-if="!route.meta.hidden"></Tabbar>
- 通过css来隐藏
设置组件的高度为页面的高度,通过z-index和 background-color盖住tabber,然后设置在组件内滚动,滚动到底部时tabber显示出来
<template>
<div class="city">
</div>
</template>
<style scoped lang="scss">
.city {
height: 100vh;
position: relative;
z-index: 1000;
background-color: #fff;
overflow-y: auto; //city里面滚动,如果在整个页面滚动滚动到底部tabber还是会显示出来
</style>

组件滚动

页面滚动

line-height 没有单位时是相对于font-size的
.item {
margin-top: 10px;
display: flex;
flex-wrap: wrap;
.text {
padding: 4px;
font-size: 13px;
margin: 2px 10px;
background-color: rgb(216, 211, 211);
border-radius: 5px;
line-height: 1.2; //没有单位时是相对于font-size的,normalize把 line-height重置为1.15 1.15*font-size有小数时 例如13.6
//会由浏览器处理,没法处理,会导致又一遍为7,由一边为6.6,而.6有一些浏览器没法处理,会去除掉,没法平分
//设置 line-height: 1 中文会导致文字偏上,所以一般会设置1.2,文字会稍微向下移动一点,而英文则不会
}
}

给子组件绑定事件,事件会被绑定到子组件的根组件上,有多个根组件时会报错
父组件:
<homeItem2 :item="item" @click="cl(item)"></homeItem2>
子组件:
使用 dayjs 格式化时间


setDate(dayValue) 根据本地时间来指定一个日期对象的天数。 返回结果是一个时间戳,例如178125330000**,会在原先的时间基础上进行加减。**
如果为 dayValue 指定 0,那么日期就会被设置为上个月的最后一天
如果 dayValue 被设置为负数,日期会设置为上个月最后一天往前数这个负数绝对值天数后的日期
getDate() 根据本地时间,返回一个指定的日期对象为一个月中的哪一日(从 1–31)
//当前时间
const nowDate = new Date(); //Tue Mar 26 2024 15:15:32 GMT+0800 (GMT+08:00)
const startDate = formatDate(nowDate) //对时间进行处理
//后一天的时间,在原来时间的基础上+1
const newDate = new Date().setDate(nowDate.getDate() + 1); // 1711523911810
const endDate = formatDate(newDate)
//开始时间-结束时间
const stayDay = getDiffDays(nowDate, newDate)
// dayjs 专门处理时间的库
import dayjs from "dayjs";
export function formatDate(day) {
return dayjs(day).format("MM月DD日");
}
export function getDiffDays(start, end) {
return dayjs(end).diff(start, "day");
}
获取当前位置
获取后会获得经纬度,返回给服务器,由服务器返回对应的位置
const getLocation = () => {
navigator.geolocation.getCurrentPosition(
(res) => {
console.log("获取位置成功:", res);
},
(err) => {
console.log("获取位置失败:", err);
},
{
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0,
}
);
};


H5端实现下拉刷新
可滚动的区域- 页面的可视区域/页面的高-距离顶部的距离=0
document.documentElement.scrollHeight-document.documentElement.clientHeight-document.documentElement.scrollTop=0
export function useScroller() {
const isScrollToBottom = ref(false);
//页面的高
const clientHeight = ref(0);
//距离顶部的距离
const scrollTop = ref(0);
//可滚动的区域
const scrollHeight = ref(0);
function scroller() {
//页面的高
clientHeight.value = document.documentElement.clientHeight;
//距离顶部的距离
scrollTop.value = document.documentElement.scrollTop;
//可滚动的区域
scrollHeight.value = document.documentElement.scrollHeight;
console.log(clientHeight.value, scrollTop.value, scrollHeight.value);
if (scrollHeight.value - (clientHeight.value + scrollTop.value) <= 1) {
//console.log(11);
isScrollToBottom.value = true;
}
}
onMounted(() => {
window.addEventListener("scroll", scroller);
});
onUnmounted(() => {
window.removeEventListener("scroll", scroller);
});
return {
isScrollToBottom,
scrollTop,
clientHeight,
scrollHeight,
};
}
这里的滚动是整个页面的(window)滚动,当页面滚动到底部时再加载数据,而不是监听热门精选区域的滚动,

房屋详情监听的是组件的滚动,根据它内部滚动到那个区域,tabber显示激活的状态。

对useScroller 进行改造,使它也能监听组件或某个元素的滚动
import { onMounted, onUnmounted, ref } from "vue";
export function useScroller(domRef) {
console.log(domRef);
let dom = window;
const isScrollToBottom = ref(false);
//页面的高
const clientHeight = ref(0);
//距离顶部的距离
const scrollTop = ref(0);
//可滚动的区域
const scrollHeight = ref(0);
function scroller() {
if (dom == window) {
//页面的高,元素在视口中可见部分的高度
// 元素内部的高度(以像素为单位),包含内边距,但不包括边框、外边距和水平滚动条, 等于 height + padding - 水平滚动条高度
clientHeight.value = document.documentElement.clientHeight;
//距离顶部的距离 滚动条距离顶部的距离,滚动的距离
scrollTop.value = document.documentElement.scrollTop;
//可滚动的区域 HTML元素内容的总高度,包括不可见的部分,同clientHeight一样,包含内边距,不包含边框,外边距,滚动条
//包括 ::before 和 ::after 这样的伪元素的高度。
scrollHeight.value = document.documentElement.scrollHeight;
// console.log(clientHeight.value, scrollTop.value, scrollHeight.value);
} else {
clientHeight.value = dom.clientHeight;
scrollTop.value = dom.scrollTop;
scrollHeight.value = dom.scrollHeight;
}
//Math.abs(element.scrollHeight - element.clientHeight - element.scrollTop) < 1;
if (scrollHeight.value - (clientHeight.value + scrollTop.value) <= 1) {
//console.log(11);
isScrollToBottom.value = true;
}
}
onMounted(() => {
if (domRef) {
dom = domRef.value;
}
console.log(dom);
dom.addEventListener("scroll", scroller);
});
onUnmounted(() => {
dom.removeEventListener("scroll", scroller);
});
return {
isScrollToBottom,
scrollTop,
clientHeight,
scrollHeight,
};
}
实现 点击tabber滚动到页面的对应区域

- 先监听 组件的滚动
const domRef = ref();
const { scrollTop } = useScroller(domRef);
<template>
<div class="detail" ref="domRef">
</div>
</template>
<style scoped lang="scss">
/要设置滚动区域的高
.detail {
height: 100vh;
overflow-y: auto;
}
</style>
- 获取 tabber中选项对应的元素
<template>
<div class="detail" ref="domRef">
<div>
<swipe :ref="getSectionRef"></swipe>
<detailTo></detailTo>
</div>
<!-- :ref="getSectionRef" 绑定函数-->
<detailContent :ref="getSectionRef"></detailContent>
<detail1 :ref="getSectionRef"></detail1>
<detailTab v-show="isShow" @cli="cli" :indexRef="indexRef"></detailTab>
<detailTest :ref="getSectionRef"></detailTest>
<div style="height: 200px"></div>
</div>
</template>
通过给 它内部的组件绑定ref来获取组件实例,:ref = v-bind:ref,可以组件的ref绑定一个函数,组件实例会作为参数传给函数。.$el 组件的根元素
// isShow 是滚动到某个位置tabber显示出来
const isShow = computed(() => {
//console.log(scrollTop.value >= 300);
return scrollTop.value >= 300;
});
const selections = [];
const getSectionRef = (value) => {
if (!value) return;
// console.log(value.$el);
selections.push(value?.$el); //alue?.$el 组件的根元素
};
selections:
3. 点击tabber滚动到对应的位置
点击tabber后把对应的index传给父元素

父元素获取index后,根据index找出对应的元素,并滚动到它的位置。
const cli = (value) => {
console.log("滚动的具体", scrollTop.value);
console.log("距离顶部的距离", selections[value].offsetTop);
console.log("距离顶部的距离", selections[value].offsetParent.offsetTop);
console.log(selections[value]);
console.log(selections[value].offsetTop - 46); //selections[value].offsetTop - 46 46 van-tab的高,保证tab不会盖住title
1. 如果是第一个元素滚动到底部
if (value == 0) {
domRef.value.scrollTo({
top: 0,
behavior: "smooth",
});
return;
}
//offsetTop是相对含有offsetParent属性的父元素来获取。而含有定位属性(position)的元素或者 本身就是 table,td,th,body 的元素,就具备 offsetParent的属性
//在获取 offsetTop的时候,会一层层往上找offsetParent,如果存在,则返回相对该元素的顶部内边距的距离;如果 不存在,则 最终 相对 body
//当元素的 style.display 设置为 "none" 时,offsetParent 返回 null,且 offetTop 返回 0
2.使用scrollTo滚动到对应的位置。offsetTop元素距离顶部的距离
domRef.value.scrollTo({
top: selections[value].offsetTop - 50,
behavior: "smooth", //平滑滚动
});
};
selections[value].offsetTop - 50 是因为tabber的高度为50,实现tabber的底部滚动到对应的元素的位置,而不是顶部滚动到元素的位置。

实现页面滚动到对应的位置,tabber对应的选项也会激活

// 记录是否第一获取内部组件的offsetTop,避免重复获取
const isFirst = ref(true);
// 用来获取内部组件的offsetTop
let list = []; //[46,411,693,1011]
// 匹配到的内部组件的下标
let start = 0;
const indexRef = ref();
watch(scrollTop, (newValue) => {
1.// 用来获取内部组件的offsetTop
if (isFirst.value) {
const val = Object.values(selections); //{0:div,1:div,2:div} 通过Object.values来获取组件实例
list = val.map((item) => item.offsetTop - 60); // -60 是因为想要tabar的底部靠近tilte时显示隐藏,是减去tbbar的高(tabber实际上不是60,只是这里写成了60)
isFirst.value = false;
}
2. 寻找第一个组件距离顶部的距离大于滚动距离的下标
for (let i = 0; i < list.length; i++) {
//找到后返回下标-1
if (list[i] >= newValue) {
start = i - 1;
break;
} else {
//没找到,就是最后一个
start = list.length - 1;
}
}
// console.log(start);
indexRef.value = start;
//可以把它传给子组件
//也可以使用 通过组件实例,在组件里写方法改变index,在通过组件实例修改index xxx.value.setsss( indexRef.value)
});
3. 找到后把它传给tabber <detailTab v-show="isShow" @cli="cli" :indexRef="indexRef"></detailTab>
4. 再tabber内部改变 Index
const props = defineProps(["indexRef"]);
watch(
() => props.indexRef,
(newValue) => {
active.value = newValue;
}
);
watch不能直接侦听响应式对象的属性值,props它是一个对象,所以需要用一个返回该属性的 getter 函数

使用keep-alive 缓存某个页面
首次进入页面后会发送网络请求,离开后再次进入这个页面还会发送网络请求,这时需要使用缓存
<RouterView v-slot="props">
<keep-alive includes="home">
<component :is="props.Component"></component>
</keep-alive>
</RouterView>
//includes="home",要使用组件名称,非setup形式可以定义组件名称,setup形式只能再写script脚本导出组件
<script>
export default {
name: "home",
};
<script setup>
</script>
//setDate() 方法根据本地时间来指定一个日期对象的天数。
var theBigDay = new Date(2024, 6, 10);
var date = theBigDay.setDate(24);
console.log(date); // 1721750400000
//根据本地时间,返回一个指定的日期对象为一个月中的哪一日(从 1--31)。
const birthday = new Date("August 19, 1975 23:15:30");
const date1 = birthday.getDate();
console.log(date1); //19
// offsetTop:用于获取元素在页面中的位置信息 ,可以用于执行基于元素位置的操作,比如滚动到特定位置。
// scrollTo(): 将页面滚动到指定的坐标位置, 它可以接受两个参数:x 和 y,分别表示页面在水平和垂直方向要滚动到的位置。以是整数值或字符串值,字符串值可以是"start"、"center"或"end",
//scrollHeight:只读属性,用于获取元素的实际内容高度,无论是否在视口内可见。通常用于在处理滚动操作时确定滚动区域的总高度,通过比较元素的scrollHeight与其实际高度,可以判断是否存在溢出的内容
//scrollTop:获取或设置一个元素的垂直滚动位置 ,滚动条顶部距离元素顶部的距离 ,距离顶部的距离
//clientHeight:只读属性,可以获取元素内部可见区域的高度
//scrollX和pageXOffset 获取当前页面水平滚动条的位置 ,获取整个页面水平滚动位置 scrollTop用于获取特定元素垂直滚动位置的属性
//offsetWidth 只读属性 获取一个元素在垂直方向上的高度, 包括元素的内容区域、内边距以及边框的高度 返回的是元素的整体高度
//clientTop 只读属性 获取一个元素上方边框的宽度, 用于获取元素上边框的宽度
</script>
3500




被折叠的 条评论
为什么被折叠?



